====== Java FTP ====== * [[:java|Java]] 에서 FTP 프로토콜 사용은 [[https://commons.apache.org/proper/commons-net/|apache commons net]] 으로 할 수 있다. * [[https://mvnrepository.com/artifact/commons-net/commons-net|commons-net maven repo]] ===== apache commons net FTPClient ===== * [[https://stackoverflow.com/a/8154539/1051402|요청 결과 상태 확인]] ''FtpClient#getReplyCode()'' * ''FTPReply'' 로 의미 확인 가능. * [[https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes|FTP Server Return Codes]] 참조해서 확인 * ''FtpClient.setFileType(FTP.BINARY_FILE_TYPE);'' : binary 전송 ===== Passive Mode ===== * FTP는 port 를 21번 명령전송 포트 22번 데이터 전송 포트 두개를 여는데, 이 중에 포트 한개만 열려있을 때 passive 모드로 변경한다. FTPClient.enterLocalPassiveMode(); ===== Binary File 전송 ===== FTPClient.setFileType(FTP.BINARY_FILE_TYPE); ===== MockFtpServer ===== * [[http://mockftpserver.sourceforge.net/|MockFtpServer]] FTP 서버를 mocking하여 테스트를 할 수 있다. public class FtpClientIntegrationTest { private FakeFtpServer fakeFtpServer; private FtpClient ftpClient; @Before public void setup() throws IOException { fakeFtpServer = new FakeFtpServer(); fakeFtpServer.addUserAccount(new UserAccount("user", "password", "/data")); FileSystem fileSystem = new UnixFakeFileSystem(); fileSystem.add(new DirectoryEntry("/data")); fileSystem.add(new FileEntry("/data/foobar.txt", "abcdef 1234567890")); fakeFtpServer.setFileSystem(fileSystem); fakeFtpServer.setServerControlPort(0); fakeFtpServer.start(); ftpClient = new FtpClient("localhost", fakeFtpServer.getServerControlPort(), "user", "password"); ftpClient.open(); } @After public void teardown() throws IOException { ftpClient.close(); fakeFtpServer.stop(); } } ===== 참조 ===== * [[https://www.baeldung.com/java-ftp-client|Implementing a FTP-Client in Java]] * [[https://jeong-pro.tistory.com/136|자바 애플리케이션에서 FTP 파일 전송하는 방법 upload sample (Apache commons-net 라이브러리)]]