Upload and download using FTPClient in JAVA
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 the commons-net package) in jakarta commons to upload and download files.
1. Upload files
I won't introduce the principle, please read the code directly
/** * Description: Upload files to the FTP server * @Version1.0 Jul 27, 2008 4:31:09 PM by Cui Hongbao (cuihongbao@) Created * @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 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; (url, port);//Connect the FTP server //If the default port is used, you can directly connect to the FTP server by using (url) (username, password);//Log in reply = (); if (!(reply)) { (); return success; } (path); (filename, input); (); (); success = true; } catch (IOException e) { (); } finally { if (()) { try { (); } 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 public void testUpLoadFromDisk(){ try { FileInputStream in=new FileInputStream(new File("D:/")); boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "", in); (flag); } catch (FileNotFoundException e) { (); } }
2. Generate a file on the FTP server and write a string to the file
@Test public void testUpLoadFromString(){ try { InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8")); boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "", input); (flag); } catch (UnsupportedEncodingException e) { (); } }
2. Download the file
The code for downloading files from the FTP server is also very simple, please refer to it as follows:
/** * Description: Download file from FTP server * @Version1.0 Jul 27, 2008 5:32:36 PM by Cui Hongbao (cuihongbao@) Created * @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 FTP server * @param fileName fileName filename to download * @param localPath The path saved to the local area after downloading * @return */ public static boolean 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; (url, port); //If the default port is used, you can directly connect to the FTP server by using (url) (username, password);//Log in reply = (); if (!(reply)) { (); return success; } (remotePath);//Transfer to FTP server directory FTPFile[] fs = (); for(FTPFile ff:fs){ if(().equals(fileName)){ File localFile = new File(localPath+"/"+()); OutputStream is = new FileOutputStream(localFile); ((), is); (); } } (); success = true; } catch (IOException e) { (); } finally { if (()) { try { (); } catch (IOException ioe) { } } } return success; }