In JAVA programs, you often need to deal with FTP, such as uploading and downloading files to the FTP server. This article briefly introduces how to use the FTPClient in jakarta commons (in the commons-net package) to upload and download files.
The jar packages used are:
commons-net-1.4.1.jar
jakarta-oro.jar
1. Upload files
File upload source code/** * Description: Upload file to the FTP server* @Version1.0 * @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 filename uploaded to the FTP server* @param input Input stream* @return Return true successfully, otherwise false */ public static boolean uploadFile( String url,//FTP server hostname int port,//FTP server port String username, // FTP login account String password, // FTP login password String path, // FTP server saves the directory String filename, // Upload to the FTP server InputStream input // Input stream) { 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 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; } Here are the test cases for file upload:
/** * Upload local files to the FTP server* */ public void testUpLoadFromDisk(){ try { FileInputStream in=new FileInputStream(new File("D:/test.txt")); boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * Generate a file on the FTP server and write a string to the file * */ public void testUpLoadFromString(){ try { String str = "This is the string to be written!"; InputStream input = new ByteArrayInputStream(str.getBytes("utf-8")); boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", input); System.out.println(flag); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } 2. File download <br />File download source code
/** * Description: Download the file from the FTP server* @Version1.0 * @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 download* @param localPath The path to save to the local after downloading* @return */ public static boolean downFile( String url, //FTP server hostname int port,//FTP server port String username, //FTP login account String password, //FTP login password String remotePath,//Relative path on the FTP server String fileName,//File name to be downloaded String localPath//Path saved to the local after downloading) { 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; } Here are the test cases for file download:
/** * Download the file on the FTP server to the local * */ public void testDownFile(){ try { boolean flag = downFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", "D:/"); System.out.println(flag); } catch (Exception e) { e.printStackTrace(); } }The above is all about this article, I hope it will be helpful to everyone's learning.