티스토리 뷰
-----------------------------------------------------------------------------------------------------------------------------------
- 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 |