Use the apache ftp tool to upload, download and delete files, as follows
1. Download the corresponding jar package
commons-net-1.4.1.jar
2. The implementation code is as follows:
public class FtpUtils { //ftp server address public String hostname = "192.168.1.249"; //ftp server port number defaults to 21 public Integer port = 21; //ftp login account public String username = "root"; //ftp login password public String password = "123"; public FTPClient ftpClient = null; /** * Initialize ftp server*/ public void initFtpClient() { ftpClient = new FTPClient(); ftpClient.setControlEncoding("utf-8"); try { System.out.println("connecting...ftp server:"+this.hostname+":"+this.port); ftpClient.connect(hostname, port); //Connect the ftp server ftpClient.login(username, password); //Login to the ftp server int replyCode = ftpClient.getReplyCode(); //Whether the server is logged in successfully if(!FTPReply.isPositiveCompletion(replyCode)){ System.out.println("connect failed...ftp server:"+this.hostname+":"+this.port); } System.out.println("connect successfu...ftp server:"+this.hostname+":"+this.port); }catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } /** * Upload file* @param pathname ftp service save address* @param fileName filename uploaded to ftp* @param originfilename The name of the file to be uploaded (absolute address) * * @return */ public boolean uploadFile( String pathname, String fileName,String originfilename){ boolean flag = false; InputStream inputStream = null; try{ System.out.println("Start upload file"); inputStream = new FileInputStream(new File(originfilename)); initFtpClient(); ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); CreateDirecroty(pathname); ftpClient.makeDirectory(pathname); ftpClient.changeWorkingDirectory(pathname); ftpClient.storeFile(fileName, inputStream); inputStream.close(); ftpClient.logout(); flag = true; System.out.println("Upload file successfully"); }catch (Exception e) { System.out.println("Upload file failed"); e.printStackTrace(); } finally{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); }catch(IOException e){ e.printStackTrace(); } } if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /** * Upload file* @param pathname ftp service save address* @param fileName File name uploaded to ftp* @param inputStream Input file stream* @return */ public boolean uploadFile( String pathname, String fileName,InputStream inputStream){ boolean flag = false; try{ System.out.println("Start upload file"); initFtpClient(); ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); CreateDirecroty(pathname); ftpClient.makeDirectory(pathname); ftpClient.changeWorkingDirectory(pathname); ftpClient.storeFile(fileName, inputStream); inputStream.close(); ftpClient.logout(); flag = true; System.out.println("Upload file successfully"); } catch (Exception e) { System.out.println("Upload file failed"); e.printStackTrace(); } finally{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); } catch(IOException e){ e.printStackTrace(); } } if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return true; } //Change the directory path public boolean changeWorkingDirectory(String directory) { boolean flag = true; try { flag = ftpClient.changeWorkingDirectory(directory); if (flag) { System.out.println("enter folder" + directory + "Success!"); } else { System.out.println("enter folder" + directory + "Failed! Start creating folder"); } } catch (IOException ioe) { ioe.printStackTrace(); } return flag; } //Create a multi-layer directory file. If there is an ftp server, it will not be created. If there is no, create public boolean CreateDirecroty(String remote) throws IOException { boolean success = true; String directory = remote + "/"; // If the remote directory does not exist, create a remote server directory recursively if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) { int start = 0; int end = 0; if (directory.startsWith("/")) { start = 1; } else { start = 0; } end = directory.indexOf("/", start); String path = ""; String paths = ""; while (true) { String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1"); path = path + "/" + subDirectory; if (!existFile(path)) { if (makeDirectory(subDirectory)) { changeWorkingDirectory(subDirectory); } else { System.out.println("Create directory[" + subDirectory + "] failed"); changeWorkingDirectory(subDirectory); } } else { changeWorkingDirectory(subDirectory); } paths = paths + "/" + subDirectory; start = end + 1; end = directory.indexOf("/", start); // Check whether all directories have been created if (end <= start) { break; } } } return success; } // Determine whether the ftp server file exists public boolean existFile(String path) throws IOException { boolean flag = false; FTPFile[] ftpFileArr = ftpClient.listFiles(path); if (ftpFileArr.length > 0) { flag = true; } return flag; } //Create directory public boolean makeDirectory(String dir) { boolean flag = true; try { flag = ftpClient.makeDirectory(dir); if (flag) { System.out.println("Create folder" + dir + "Success!"); } else { System.out.println("Create folder" + dir + "Failed!"); } } catch (Exception e) { e.printStackTrace(); } return flag; } /** * Download file* * @param pathname FTP server file directory* * @param filename File name* * @param localpath File path after download* * @return */ public boolean downloadFile(String pathname, String filename, String localpath){ boolean flag = false; OutputStream os=null; try { System.out.println("Start download file"); initFtpClient(); //Switch the FTP directory ftpClient.changeWorkingDirectory(pathname); FTPFile[] ftpFiles = ftpClient.listFiles(); for(FTPFile file : ftpFiles){ if(filename.equalsIgnoreCase(file.getName())){ File localFile = new File(localpath + "/" + file.getName()); os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); } } ftpClient.logout(); flag = true; System.out.println("Download file successfully"); } catch (Exception e) { System.out.println("Download file failed"); e.printStackTrace(); } finally{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); } catch(IOException e){ e.printStackTrace(); } } if(null != os){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return flag; } /** * Delete the file* * @param pathname FTP server saves directory* * @param filename The file name to be deleted* * @return */ public boolean deleteFile(String pathname, String filename){ boolean flag = false; try { System.out.println("Start delete file"); initFtpClient(); //Switch the FTP directory ftpClient.changeWorkingDirectory(pathname); ftpClient.dele(filename); ftpClient.logout(); flag = true; System.out.println("Delete file successfully"); } catch (Exception e) { System.out.println("Delete file failed"); e.printStackTrace(); } finally { if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); } catch(IOException e){ e.printStackTrace(); } } return flag; } public static void main(String[] args) { FtpUtils ftp =new FtpUtils(); //ftp.uploadFile("ftpFile/data", "123.docx", "E://123.docx"); //ftp.downloadFile("ftpFile/data", "123.docx", "F://"); ftp.deleteFile("ftpFile/data", "123.docx"); System.out.println("ok"); } } The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.