FTP 전송하기

2011/08/23 18:04 from Program(Java)
FTP 전송 프로그램을 작성하기 위해서는 Apache 그룹에서 제공해주는 lib를 사용해서 작성해야 한다.
commons-net 라이브러리 이며
apache.org에서  다운 받을 수 있다.
현재 가장 최신버젼은 3.0.1 버젼이다.

사용하는 클래스
 

1. FTPClient 
 메소드명 설명 
connect(String host, int port)   ftp에 접속하기 위한 메소드
login(String user, String pwd)  접속한 ftp에 로그인 하기위한 메소드
changeWorkingDirectory(String dirPath)   작업 디렉토리 변경하는 메소드
listDirectories()  현재 작업 디렉토리에서의 디렉토리 리스트를 보는 메소드
listDirectories(String dirPath)   파라메터로 받은 디렉토리에서의 디렉토리 리스트
storeFile(String fileName, FileInputStream fis)   fileName : ftp에 저장 되는 이름
 fis : 로컬 파일을 FileInputStream으로 변환한 것
retreiveFile(String fileName, FileOutputStream fos)  fileName : ftp에 저장 되어있는 파일 이름
 fos : 로컬에 저장될 파일을 생성해서 FileOutputStream으로 변환한 것

2. FTPFile 
FTPClient class 의 listDirectories, listFiles 메소드의 리턴 값.



이제부터 작성법을 간단하게 보도록 하겠다.
일단 ftp에 접속하고 로그인 하는 부분은 정말 쉽다...
아무런 것도 없다,,,
상단에 적은 FTPClient class 의 connect, login 메소드만 사용하면 된다.
일단 connect로 접속 하고 login으로 로그인 하고..
얼마나 간단한가?  소스 코드로 한번 봐 보자....

FTPClient ftp = new FTPClient();
ftp.connect("192.168.0.1","23");
ftp.login("user","pwd");


디렉토리 이동 또한 매우 간단하다...

ftp.changeWorkingDirectory
("/home/gaury");
 

put의 경우 
로컬에 있는 파일을 FileInputSream에 담아 storeFile을 하면 된다.

File localFile = new File(localDir, fileName);
FileInputStream  fis = new FileInputStream(localFile);
ftp.storeFile(fileName, fis);


get의 경우
로컬에 담을 경로 및 파일 명을 FileOutPutStream에 담고 retrieveFile를 하면 된다.

File localFile = new File(localDir, fileName);
FileOutputStream fos = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, fos);

위의 소스들이 가장 기본이 되는 것들이다..
다른 기능들을 하는 메소드들도 많지만 그것들은 직접 찾아보면서 작업 하기 바란다.

저작자 표시 비영리 변경 금지
크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 침묵의 함대 트랙백 0 : 댓글 0