Based on Apache FTP, the following issues need to be considered when uploading files (the example is the continuation function):
(1) Whether the FTP server has a directory change, if it does not exist, you need to create a directory.
(2) Determine whether the uploaded file already exists. If it exists, does it need to be deleted before uploading or continued transmission.
1. Upload or download the enumeration class:
package com.scengine.wtms.utils.ftp; public enum UploadStatus { File_Exits(0), Create_Directory_Success(1), Create_Directory_Fail(2), Upload_From_Break_Success(3), Upload_From_Break_Faild(4), Download_From_Break_Success(5), Download_From_Break_Faild(6), Upload_New_File_Success(7), Upload_New_File_Failed(8), Delete_Remote_Success(9), Delete_Remote_Faild(10),Remote_Bigger_Local(11),Remote_smaller_locall(12); private int status; public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } UploadStatus(int status) { this.status = status; } }2. Tool code:
package com.scengine.wtms.utils.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class ContinueFTP { private FTPClient ftpClient = new FTPClient(); /** * Object construction settings output the commands used in the process to the console*/ public ContinueFTP() { this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); } /** * * Used to connect to the FTP server in java programming* * @param hostname * Hostname * @param port * Port * @param username * Username * @param password * Password * @return Whether the connection is successful* * @throws IOException */ public boolean connect(String hostname, int port, String username, String password) throws IOException { ftpClient.connect(hostname, port); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { if (ftpClient.login(username, password)) { return true; } } disconnect(); return false; } /** * * Download file from the FTP server* * @param remote * Remote file path* * @param local * Local file path* * @return Whether it is successful* * @throws IOException */ @SuppressWarnings("resource") public boolean download(String remote, String local) throws IOException { ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); boolean result; File f = new File(local); FTPFile[] files = ftpClient.listFiles(remote); if (files.length != 1) { System.out.println("Remote file is not unique"); return false; } long lRemoteSize = files[0].getSize(); if (f.exists()) { OutputStream out = new FileOutputStream(f, true); System.out.println("The local file size is:" + f.length()); if (f.length() >= lRemoteSize) { System.out.println("The local file size is greater than the remote file size, download aborted"); return false; } ftpClient.setRestartOffset(f.length()); result = ftpClient.retrieveFile(remote, out); out.close(); } else { OutputStream out = new FileOutputStream(f); result = ftpClient.retrieveFile(remote, out); out.close(); } return result; } /** * * Upload file to the FTP server, support breakpoint continuous transmission* * @param local * Local file name, absolute path* * @param remote * Remote file path, use /home/directory1/subdirectory/file.ext * According to the path specified on Linux, support multi-level directory nesting, support recursively creating non-existent directory structures* * @return Upload result* * @throws IOException */ @SuppressWarnings("resource") public UploadStatus upload(String local, String remote) throws IOException { // Set PassiveMode to transmit ftpClient.enterLocalPassiveMode(); // Set ftpClient.setFileType(FTP.BINARY_FILE_TYPE); UploadStatus result; // Processing of remote directories String remoteFileName = remote; if (remote.contains("/")) { remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); String directory = remote.substring(0, remote.lastIndexOf("/") + 1); if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(directory)) { // If the remote directory does not exist, create the remote server directory recursively int start = 0; int end = 0; if (directory.startsWith("/")) { start = 1; } else { start = 0; } end = directory.indexOf("/", start); while (true) { String subDirectory = remote.substring(start, end); if (!ftpClient.changeWorkingDirectory(subDirectory)) { if (ftpClient.makeDirectory(subDirectory)) { ftpClient.changeWorkingDirectory(subDirectory); } else { System.out.println("Create failed to create directory"); return UploadStatus.Create_Directory_Fail; } } start = end + 1; end = directory.indexOf("/", start); // Check whether all directories have been created if (end <= start) { break; } } } } // Check whether the file exists in the remote FTPFile[] files = ftpClient.listFiles(remoteFileName); if (files.length == 1) { long remoteSize = files[0].getSize(); File f = new File(local); long localSize = f.length(); if (remoteSize == localSize) { return UploadStatus.File_Exits; } else if (remoteSize > localSize) { return UploadStatus.Remote_Bigger_Local; } // Try to move the pointer in the file to realize the interruption of breakpoint InputStream is = new FileInputStream(f); if (is.skip(remoteSize) == remoteSize) { ftpClient.setRestartOffset(remoteSize); if (ftpClient.storeFile(remote, is)) { return UploadStatus.Upload_From_Break_Success; } } // If the interruption of breakpoint does not succeed, delete the file on the server and upload it again if (!ftpClient.deleteFile(remoteFileName)) { return UploadStatus.Delete_Remote_Faild; } is = new FileInputStream(f); if (ftpClient.storeFile(remote, is)) { result = UploadStatus.Upload_New_File_Success; } else { result = UploadStatus.Upload_New_File_Failed; } is.close(); } else { InputStream is = new FileInputStream(local); if (ftpClient.storeFile(remoteFileName, is)) { result = UploadStatus.Upload_New_File_Success; } else { result = UploadStatus.Upload_New_File_Failed; } is.close(); } return result; } /** * * Disconnect from the remote server* * @throws IOException */ public void disconnect() throws IOException { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } public static void main(String[] args) { ContinueFTP myFtp = new ContinueFTP(); try { myFtp.connect("192.168.1.200", 21, "duser", "HTPDuserXP32"); System.out.println(myFtp.upload("C://Users//Administrator//Desktop//swing.drawer.jar", "/jars/swing.drawer.jar")); myFtp.disconnect(); } catch (IOException e) { System.out.println("Error connecting to FTP: " + e.getMessage()); } } } 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.