티스토리 뷰

개발(Dev)/(02) 서버 개발

(Java) FTP Client

낭만 고양이 2019. 8. 1. 14:37

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

- Java 소스

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

...
...

private FTPClient ftp = null;
...
...

// FTP 연결 해제
public void disconnect() throws Exception
{
	if (this.ftp.isConnected()) 
  	{
  		this.ftp.logout();
  		this.ftp.disconnect();
  	}
}
...
...

// FTP 연결
public void connect(String host, String user, String pwd) throws Exception
{
    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    ftp.connect(host);//호스트 연결
    
    int response = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(response)) 
    {
    ftp.disconnect();
    throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(user, pwd);//로그인
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
}
...
...

// FTP 파일 전송
public void uploadFile(String localFileFullName, String fileName, String hostDir) throws Exception 
{
	try(InputStream input = new FileInputStream(new File(localFileFullName)))
	{
		this.ftp.storeFile(hostDir + fileName, input); 
	}
}

 

- Maven Dependency

<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.6</version>
</dependency>

 

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

'개발(Dev) > (02) 서버 개발' 카테고리의 다른 글

(JAVA) ObjectMapper 데이터 변환  (0) 2020.06.02
(JAVA) java.util.stream 기능  (0) 2020.06.02
(Java) SFTP Client  (0) 2019.08.01
(Java) bCript 암호화  (0) 2019.08.01
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함