FTPClientUtil

FTPClientUtil

FTPClientUtil.java
--------------------------------------------

package common.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * FTP 클라이언트 유틸리티
 * @author uiseokj
 */

public class FTPClientUtil {
 /** 파일명 컨트롤 인코딩 */
 private String  encoding   = "euc-kr";
 /** FTP 서버 주소 */
 private String  address    = null;
 /** FTP 연결 포트 */
 private int   port    = 21;
 /** FTP 계정 ID */
 private String  id     = null;
 /** FTP 계정 비밀번호 */
 private String  password   = null;
 /** 업로할 디렉토리(서버) */
 private String  uploadDirectory  = "./";
 /** 다운로드할 디렉토리(로컬) */
 private String  downloadDirectory = "./";
 /** FTP Client 객체 */
 private FTPClient ftpClient   = null;

 public FTPClientUtil() {
  ftpClient = new FTPClient();
 }

 public FTPClientUtil(String ip, String id, String pwd) {
  this(ip, 21, id, pwd);
 }

 public FTPClientUtil(String ip, int port, String id, String pwd) {
  this();
  setAddress(ip);
  setPort(port);
  setId(id);
  setPassword(pwd);
 }

 /**
  * FTP 서버에 연결
  */
 public void connect() {
  try {
   ftpClient.connect(address, port);
   int reply;
   // 연결 시도후, 성공했는지 응답 코드 확인
   reply = ftpClient.getReplyCode();
   if (!FTPReply.isPositiveCompletion(reply)) {
    ftpClient.disconnect();
   }
  } catch (IOException ioe) {
   if (ftpClient.isConnected()) {
    try {
     ftpClient.disconnect();
    } catch (IOException f) {
    }
   }
  }
 }

 /**
  * 계정과 비밀번호로 로그인
  * @return
  */
 public boolean login() {
  try {
   return ftpClient.login(id, password);
  } catch (IOException ioe) {
  }
  return false;
 }

 /**
  * 로그아웃
  * @return
  */
 public boolean logout() {
  try {
   return ftpClient.logout();
  } catch (IOException ioe) {
  }
  return false;
 }

 /**
  * FTP 연결 종료
  */
 public void disconnect() {
  try {
   ftpClient.disconnect();
  } catch (IOException ioe) {
   ioe.printStackTrace();
  }
 }

 /**
  * FTP의 ls 명령, 모든 파일 리스트를 가져온다
  */
 public FTPFile[] list() {
  try {
   return (FTPFile[]) this.ftpClient.listFiles();
  } catch (IOException ioe) {
  }
  return null;
 }

 /**
  * FTP 서버로 부터 주어진 이름의 파일 다운로드
  * @param srcPath 원본 파일 패스(서버)
  * @return 결과
  */
 public boolean get(String srcPath) {
  File file = new File(srcPath);
  return get(srcPath, downloadDirectory + file.getName());
 }

 /**
  * FTP 서버로 부터 주어진 이름의 파일 다운로드
  * @param srcPath 원본 파일 패스(서버)
  * @param destPath 저장할 파일명(로컬)
  * @return 결과
  */
 public boolean get(String srcPath, String destPath) {
  boolean successFlag = false;
  OutputStream output = null;
  try {
   //로컬 디렉토리에 ,저장할 파일명으로 FileOutputStream 생성
   File file = new File(destPath);
   output = new FileOutputStream(file);
   successFlag = ftpClient.retrieveFile(srcPath, output);
  } catch (FileNotFoundException fnfe) {
   successFlag = false;
  } catch (IOException ioe) {
   successFlag = false;
  } finally {
   try {
    output.close();
   } catch (IOException e) {
   }
  }
  return successFlag;
 }

 /**
  * FTP서버로 파일 업로드
  * @param srcPath 원본 파일의 경로(로컬)
  * @return 결과
  */
 public boolean put(String srcPath) {
  File file = new File(srcPath);
  return put(srcPath, uploadDirectory + file.getName());
 }

 /**
  * FTP서버로 파일 업로드
  * @param srcPath 원본 파일의 경로(로컬)
  * @param destPath 저장할 파일의 경로(서버)
  * @return 결과
  */
 public boolean put(String srcPath, String destPath) {
  boolean successFlag = false;
  InputStream input = null;
  try {
   File file = new File(srcPath);
   input = new FileInputStream(file);
   ftpClient.setFileType(2);
   successFlag = ftpClient.storeFile(destPath, input);
  } catch (FileNotFoundException fnfe) {
   successFlag = false;
  } catch (IOException e) {
   successFlag = false;
  } finally {
   try {
    input.close();
   } catch (IOException e) {
   }
  }
  return successFlag;
 }

 /**
  * 원격 디렉토리 이동
  * @param path 이동할 디렉토리
  */
 public boolean cd(String path) {
  try {
   return ftpClient.changeWorkingDirectory(path);
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
 }

 /**
  * 원격 파일명 변경
  * @param srcPath 원본 파일 패스(서버)
  * @param destPath 변경된 파일명 패스(서버)
  * @return
  */
 public boolean rename(String srcPath, String destPath) {
  try {
   return ftpClient.rename(srcPath, destPath);
  } catch (IOException e) {
   e.printStackTrace();
  }
  return false;
 }

 /**
  * 원격 파일 삭제
  * @param path 삭제할 파일 패스
  * @return
  */
 public boolean deleteFile(String path) {
  try {
   return ftpClient.deleteFile(path);
  } catch (IOException e) {
   e.printStackTrace();
  }
  return false;
 }

 /**
  * 원격 디렉토리 생성
  * @param path 디렉토리 패스
  * @return
  */
 public boolean makeDirectory(String path) {
  try {
   return ftpClient.makeDirectory(path);
  } catch (IOException e) {
   e.printStackTrace();
  }
  return false;
 }

 /**
  * FTP 서버 주소 리턴
  * @return Returns the address.
  */
 public String getAddress() {
  return address;
 }

 /**
  * 로컬 다운로드 디렉토리 패스 리턴
  * @return Returns the downloadDirectory.
  */
 public String getDownloadDirectory() {
  return downloadDirectory;
 }

 /**
  * FTP 컨트롤을 위한 인코딩 리턴
  * @return Returns the encoding.
  */
 public String getEncoding() {
  return encoding;
 }

 /**
  * <code>FTPClient</code> 가져오기
  * @return Returns the ftpClient.
  */
 public FTPClient getFtpClient() {
  return ftpClient;
 }

 /**
  * FTP 계정 아이디 리턴
  * @return Returns the id.
  */
 public String getId() {
  return id;
 }

 /**
  * FTP 계정 비밀번호 리턴
  * @return Returns the password.
  */
 public String getPassword() {
  return password;
 }

 /**
  * FTP 연결 포트 리턴
  * @return Returns the port.
  */
 public int getPort() {
  return port;
 }

 /**
  * FTP 서버 측 업로드 디렉토리 경로 리턴
  * @return Returns the uploadDirectory.
  */
 public String getUploadDirectory() {
  return uploadDirectory;
 }

 /**
  * FTP 서버 주소 설정
  * @param address The address to set.
  */
 public void setAddress(String address) {
  this.address = address;
 }

 /**
  * 로컬 다운로드 디렉토리 설정
  * @param downloadDirectory The downloadDirectory to set.
  */
 public void setDownloadDirectory(String downloadDirectory) {
  this.downloadDirectory = downloadDirectory;
 }

 /**
  * FTP 컨트롤 위한 인코딩 설정
  * @param encoding The encoding to set.
  */
 public void setEncoding(String encoding) {
  this.encoding = encoding;
 }

 /**
  * FTPClient 설정
  * @param ftpClient The ftpClient to set.
  */
 public void setFtpClient(FTPClient ftpClient) {
  this.ftpClient = ftpClient;
 }

 /**
  * FTP 계정 아이디 설정
  * @param id The id to set.
  */
 public void setId(String id) {
  this.id = id;
 }

 /**
  * FTP 계정 비밀번호 설정
  * @param password The password to set.
  */
 public void setPassword(String password) {
  this.password = password;
 }

 /**
  * FTP 연결 포트 설정
  * @param port The port to set.
  */
 public void setPort(int port) {
  this.port = port;
 }

 /**
  * FTP 서버 측 업로드 디렉토리 경로 설정
  * @param uploadDirectory The uploadDirectory to set.
  */
 public void setUploadDirectory(String uploadDirectory) {
  this.uploadDirectory = uploadDirectory;
 }
}

--------------------------------------------


사용 예제 :

  String ip = "211.233.81.21";
  String id = "tlink";
  String pw = "roskfldnpq4";
try {
   FTPClientUtil ftp = new FTPClientUtil(ip, id, pw);
   ftp.connect();
   ftp.login();

   ftp.put(local, server);

   ftp.logout();
   ftp.disconnect();
  } catch(Exception e) {
   success = false;
   throw e;
  }

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받고 있습니다.

이 포스팅은 제휴마케팅이 적용되어 작성자에게 일정액의 커미션이 제공될수 있습니다.

이 글을 공유하기

댓글

Designed by JB FACTORY

"웨딩박람회 일정 스드메 견적 웨딩플랜닷컴 "

주부알바 재택부업 앙팡펫파트너스

서민안심전환대출 ㅣ정부지원대출ㅣ채무통합대환대출