1. FTP Client Examples
Download: commons-net-3.6
FtpClient.java
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public class FtpClient { FTPClient ftp = null; public FtpClient(String host, String user, String pwd) throws Exception{ ftp = new FTPClient(); ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); ftp.connect(host); int reply = ftp.getReplyCode(); if( !FTPReply.isPositiveCompletion(reply) ) { ftp.disconnect(); throw new Exception("Exception in connecting to FTP Server"); } ftp.login(user, pwd); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); } public void uploadFile(String localFileFullName, String remoteFileName) throws Exception { try( InputStream input = new FileInputStream(new File(localFileFullName)) ) { this.ftp.storeFile(remoteFileName, input); } } public void downloadFile(String localFileFullName, String remoteFileName) throws Exception { try( OutputStream output = new FileOutputStream(new File(localFileFullName)) ) { this.ftp.retrieveFile(remoteFileName, output); } } public void makeDirectory(String remoteDirName) throws Exception { this.ftp.makeDirectory(remoteDirName); } public void removeDirectory(String remoteDirName) throws Exception { this.ftp.removeDirectory(remoteDirName); } public void deleteFile(String remoteFileName) throws Exception { this.ftp.deleteFile(remoteFileName); } public void getList(String path) throws IOException { FTPFile[] ff = ftp.listFiles(); for( FTPFile f : ff ) { if( f.isDirectory() ) System.out.print( "["); System.out.print(f.getName()); if( f.isDirectory() ) System.out.print( "]"); System.out.print( " " + f.getSize()); System.out.print( " " + f.getTimestamp().getTime()); System.out.println(); } } public void disconnect() { if( this.ftp.isConnected() ) { try { this.ftp.logout(); this.ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } |
Usage:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | static void testUpload() { try { FtpClient fc = new FtpClient("192.168.0.1", "user", "password"); fc.uploadFile("D:\\oui_org.txt", "oui.txt"); fc.disconnect(); } catch (Exception e) { e.printStackTrace(); } } static void testDownload() { try { FtpClient fc = new FtpClient("192.168.0.1", "user", "password"); fc.downloadFile("D:\\oui_xxx.txt", "oui.txt"); fc.disconnect(); } catch (Exception e) { e.printStackTrace(); } } static void testMakeDirectory() { try { FtpClient fc = new FtpClient("192.168.0.1", "user", "password"); fc.makeDirectory("xxxx-yyyy"); fc.disconnect(); } catch (Exception e) { e.printStackTrace(); } } static void testRemoveDirectory() { try { FtpClient fc = new FtpClient("192.168.0.1", "user", "password"); fc.removeDirectory("xxxxyyyy"); fc.disconnect(); } catch (Exception e) { e.printStackTrace(); } } static void testDeleteFile() { try { FtpClient fc = new FtpClient("192.168.0.1", "user", "password"); fc.deleteFile("out.txt"); fc.disconnect(); } catch (Exception e) { e.printStackTrace(); } } static void testGetList() { try { FtpClient fc = new FtpClient("192.168.0.1", "user", "password"); fc.getList("/home/user"); fc.disconnect(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { testUpload(); testDownload(); testMakeDirectory(); testRemoveDirectory(); testDeleteFile(); testGetList(); } |
댓글 없음:
댓글 쓰기