In Java program development, ftp is used more often and often deals with it, such as uploading and downloading files to the FTP server. This article introduces you how to use the FTPClient in jakarta commons (in the commons-net package) to upload and download files.
1. Upload files
I won't introduce the principle, please read the code directly
/** * Description: Upload file to the FTP server* @Version1.0 Jul 27, 2008 4:31:09 PM by Cui Hongbao ([email protected]) Create * @param url FTP server hostname * @param port FTP server port * @param username FTP login account * @param password FTP login password * @param path FTP server save directory * @param filename Upload to the FTP server* @param input Input stream* @return Return true successfully, otherwise return false */ publicstaticboolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port);//Connect the FTP server//If the default port is used, you can directly connect to the FTP server by using ftp.connect(url); ftp.login(username, password);//Login reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(path); ftp.storeFile(filename, input); input.close(); ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }<pre></pre> /*** Description: Upload file to the FTP server* @Version1.0 Jul 27, 2008 4:31:09 PM by Cui Hongbao ([email protected]) Create * @param url FTP server hostname* @param port FTP server port* @param username FTP login account* @param password FTP login password* @param path FTP server save directory* @param filename Upload to the FTP server* @param input Input stream* @return Return true successfully, otherwise return false*/public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {boolean success = false;FTPClient ftp = new FTPClient();try {int reply;ftp.connect(url, port);//Connect the FTP server//If the default port is used, you can directly connect to the FTP server by using ftp.connect(url);ftp.login(username, password);//Login reply = ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();return success;}ftp.changeWorkingDirectory(path);ftp.storeFile(filename, input); input.close();ftp.logout();success = true;} catch (IOException e) {e.printStackTrace();} finally {if (ftp.isConnected()) {try {ftp.disconnect();} catch (IOException ioe) {}}}return success;}
Let's write two small examples:
1. Upload the local file to the FTP server, the code is as follows:
@Test publicvoid testUpLoadFromDisk(){ try { FileInputStream in=new FileInputStream(new File("D:/test.txt")); boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } }<pre></pre> @Testpublic void testUpLoadFromDisk(){try {FileInputStream in=new FileInputStream(new File("D:/test.txt"));boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);System.out.println(flag);} catch (FileNotFoundException e) {e.printStackTrace();}} 2. Generate a file on the FTP server and write a string to the file
@Test publicvoid testUpLoadFromString(){ try { InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8")); boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input); System.out.println(flag); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }<pre></pre> @Testpublic void testUpLoadFromString(){try {InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);System.out.println(flag);} catch (UnsupportedEncodingException e) {e.printStackTrace();}} 2. Download the file
The code for downloading files from an FTP server is also very simple, please refer to it as follows:
/** * Description: Download the file from the FTP server* @Version. Jul, :: PM by Cui Hongbao ([email protected]) Create * @param url FTP server hostname * @param port FTP server port* @param username FTP login account* @param password FTP login password* @param remotePath Relative path on the FTP server* @param fileName File name to be downloaded* @param localPath The path to save to the local after downloading* @return */ publicstaticboolean downFile(String url, int port,String username, String password, String remotePath,String fileName,String localPath) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port); //If the default port is used, you can directly connect to the FTP server by using ftp.connect(url); ftp.login(username, password);//Login reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(remotePath);//Transfer to the FTP server directory FTPFile[] fs = ftp.listFiles(); for(FTPFile ff:fs){ if(ff.getName().equals(fileName)){ File localFile = new File(localPath+"/"+ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }<pre></pre>