JAVA implementation of FTP breakpoint upload method

  • 2020-04-01 03:57:09
  • OfStack

This article illustrates a JAVA method for implementing breakpoint uploading for FTP. Share with you for your reference. Specific analysis is as follows:

This is mainly implemented using the net package in apache. http://commons.apache.org/net/. See the official website for package download and API documentation. Breakpoint upload is to set the starting point of the transfer during the upload process. And set up the binary transfer.


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(); 
   public ContinueFTP(){ 
     //Sets the output of the commands used in the procedure to the console
     this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
   } 
    
   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; 
   } 
    
   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 files are 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(" Local file size greater than 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 files to FTP Server, support breakpoint continuation  
   * @param local  Local file name, absolute path  
   * @param remote  Remote file path, use /home/directory1/subdirectory/file.ext  In accordance with the Linux Path on the specified way, support multi - level directory nesting, support recursive creation of non-existent directory structure  
   * @return  Upload the result  
   * @throws IOException 
   */ 
   public UploadStatus upload(String local,String remote) throws IOException{ 
     //Set the PassiveMode transport
     ftpClient.enterLocalPassiveMode(); 
     //The Settings are transmitted as a binary stream
     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, the remote server directory is created 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(" Directory creation failed "); 
               return UploadStatus.Create_Directory_Fail;
             } 
           } 
           start = end + 1; 
           end = directory.indexOf("/",start); 
           //Check that all directories are created
           if(end <= start){ 
             break; 
           } 
         } 
       } 
     } 
     //Check for remote file existence
     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; 
       } 
       //Attempt to move the read pointer in the file to achieve breakpoint continuation
       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 breakpoint continuation does not succeed, the file is deleted from the server and re-uploaded
       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; 
   } 
    
   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.21.171", 21, "test", "test"); 
       System.out.println(myFtp.upload("E:\VP6.flv", "/MIS/video/VP6.flv")); 
       myFtp.disconnect(); 
     } catch (IOException e) { 
       System.out.println(" The connection FTP Error: "+e.getMessage()); 
     } 
   } 
}

I hope this article has been helpful to your Java programming.


Related articles: