Enterprise Distributed Technologies 의 FTPClient클래스를 이용한 Java에서의 파일업로드

|

Enterprised Distributed Technologies(이하 EDT)의 FTPClient 클래스를 이용해
자바 내에서 FTP전송(업로드)하는 방법을 알아보세~

공식 사이트는 http://www.enterprisedt.com/products/edtftpj/overview.html

그리고 API는 http://www.enterprisedt.com/products/edtftpj/doc/api/index.html

EDT의 메인페이지로 가면 HTML/JS버전과 .net을 위한 버전도 있으므로 참고하자.


먼저 연결,

FTP 접속 및 로그인 (Language : java)
  1. FTPClient ftp = new FTPClient("localhost");
  2. ftp.login("account", "password");


버뜨,
호스트를 바로 주어 연결하는건 deprecated되었단다,
인자 없이 생성하여 setter메소드를 사용하는걸 api에선 추천한다.

또 버뜨,
테스트로 개발한 녀석이 deprecated되기 전 jar라서 그냥 이대로 설명을...-_-;

어쨌든, 연결을 하고 나면 저 ftp라는 변수로 여러 작업을 할 수 있다.
자세한건 doc을 참고하고, 메소드로 만들어놓은걸 보면,


파일 업로드하기 (Language : java)
  1. private boolean TransferFile(FTPClient ftp, String toSend) {
  2.     try{
  3.         String ext = toSend.substring(toSend.lastIndexOf(".")+1).toUpperCase();
  4.         if(ext.equals("TXT")) {
  5.             // 전송모드 ASCII로 설정.
  6.             ftp.setType(FTPTransferType.ASCII);
  7.         } else {
  8.             // 전송모드 BINARY로 설정
  9.             ftp.setType(FTPTransferType.BINARY);
  10.         }
  11.  
  12.         String relativePath = toSend.substring(strClientRoot.length());
  13.  
  14.         String upPath;
  15.         if(relativePath.lastIndexOf("/") < 0)
  16.             upPath = strServerRoot;
  17.         else
  18.             upPath = strServerRoot + relativePath.substring(0, relativePath.lastIndexOf("/"));
  19.  
  20.         String [] dir = ftp.dir(upPath);
  21.         if(dir == null || dir.length == 0) {
  22.             // 업로드 패스 없음, 생성
  23.             mkDir(ftp, upPath);
  24.         }
  25.  
  26.         // 파일 업로드
  27.         ftp.put(strClientRoot+relativePath, strServerRoot+relativePath);
  28.        
  29.         return true;
  30.     }catch(Exception e) {
  31.         e.printStackTrace();
  32.         return false;
  33.     }
  34. }
  35.  


FTPClient 인스턴스와 로컬 파일명을 주면 샤삭.. 업로드 해준다.
ASCII와 BINARY는 txt확장자만 되어 있으나 if문에 html이나 htm이나 뭐 암튼 추가하면 되고....
strServerRoot나 strClientRoot.. 뭐 이런 변수는 클래스 전역으로 있는 녀석들이고... 대충 보면 알껌다.

요기서 중요한건,
전송모드(ASCII/BINARY)설정하는거랑
FTPClient.put 메소드라는거. put()메소드도 인자 따라 오버라이드 되어있으므로 역시 api참고.


저기서 사용한 mkDir()메소드는 만든겁니다.
FTPClient에도 mkdir()이라는 메소드가 있지만 2depth이상 존재하지 않을땐 여지없이 Exception을 띄우길래
허접하게나마 작성. 한 코드를 공개하자면,


리모트 디렉토리 생성 (Language : java)
  1. private boolean mkDir(FTPClient ftp, String path) {
  2.     if(path.charAt(path.length()-1) == '/')
  3.         path = path.substring(0, path.length()-1);
  4.  
  5.     String parentPath = path.substring(0, path.lastIndexOf("/"));
  6.     String [] dir;
  7.  
  8.     try{
  9.         dir = ftp.dir(parentPath);
  10.     }catch(Exception e) {
  11.         dir = null;
  12.     }
  13.  
  14.     if(dir == null || dir.length == 0) {
  15.         while(!mkDir(ftp, parentPath));
  16.         try{
  17.             ftp.mkdir(path);
  18.         }catch(Exception e) { }
  19.         return false;
  20.     } else {
  21.         try{
  22.             ftp.mkdir(path);
  23.         }catch(Exception e) { }
  24.         return true;
  25.     }
  26. }
  27.  


이래 됨다... 역시나 재귀호출이죠,
ftp.mkdir()을 다 따로 try-catch절로 묶어놓은 이유는
테스트컴 jar가 구버전이라 FTPClient.exists()메소드를 지원하지 않아서
FTPClient.dir()메소드를 사용해서 존재/미존재 여부를 파악하는데
디렉토리 내부에 파일이 없을 경우 legnth가 0이 되기 때문에 FTPClient.mkdir()을 실행합니다.
근데! 이미 존재할 경우 mkdir()에서 예외가 발생하기 땀시... 각기 따로따로...


어쨌든, 클래스 만든애들이 제공하는 apidoc만큼 정확한 정보는 없으니
api document를 참고하시고...
뭐 대충 이런게 있다라는것만... -ㅅ-; (글싸질러놓고 도망가기..)
And