Java implementation of FTP breakpoint upload method _java

Source: Internet
Author: User
Tags create directory

This article describes the Java implementation of FTP breakpoint upload method. Share to everyone for your reference. The specific analysis is as follows:

This is mainly implemented using the net package in Apache. URL http://commons.apache.org/net/. Download the specific package and API documentation please reader network. A breakpoint upload sets the starting position of the transmission during the upload process. and set the binary transmission.

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 (); The public continueftp () {//sets the command used in the procedure to output to the console This.ftpClient.addProtocolCommandListener new Printcommandlistener ( 
   New PrintWriter (System.out))); /** * Java Programming for connecting to FTP server * @param hostname host name * @param port * @param username username * @param p Assword Password * @return Whether the connection succeeded * @throws IOException/public boolean connect (String hostname,int Port,strin G username,string password) throws ioexception{Ftpclient.connect (hostname,Port); if (Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {if (Ftpclient.login (username, password)) {RE 
       Turn true; 
     } disconnect (); 
   return false; /** * Download files from FTP server * @param remote file path * @param local file path * @return Success * @throws IOEXCEP tion */public boolean download (String remote,string local) throws ioexception{Ftpclient.enterlocalpassivem 
     Ode (); 
     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 ("Local File size is:" +f.length ()); 
         if (F.length () >= lremotesize) {System.out.println ("Local file size is larger 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 continued * @param local file name, absolute path * @param remote file path, using/home/directory1/subdirecto Ry/file.ext in accordance with the path specified on Linux, support multilevel directory nesting, support recursively create nonexistent directory structure * @return upload results * @throws ioexception/public uploads Tatus upload (String local,string remote) throws ioexception{//set Passivemode transmission Ftpclient.enterlocalpassivemode 
     (); 
     A binary stream is set to transmit 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, recursively creates a remote 
         Server directory 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 directory Failed");
             return uploadstatus.create_directory_fail; 
           } start = end + 1; 
           End = Directory.indexof ("/", start); 
           Check that all directories are created if (end <= start) {break; Check the remote,}}}//Whether there is a file 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 within the file to implement the 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 breakpoint is not successful, delete the file on the server and upload if (!ftpclient.deletefile (Remotefilename)) {return Upload 
       Status.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 remote server * @throws IOException */public void disconnect () throws ioexception{if (FTP 
     Client.isconnected ()) {ftpclient.disconnect (); 
     } public static void Main (string[] args) {continueftp myftp = new Continueftp (); 
       try {myftp.connect ("192.168.21.171," "Test", "test"); 
       System.out.println (Myftp.upload ("e:\\vp6.flv", "/mis/video/vp6.flv")); 
     Myftp.disconnect (); 
     catch (IOException e) {System.out.println ("Connection FTP error:" +e.getmessage ());

 } 
   } 
}

I hope this article will help you with your Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.