Java implementation of FTP file upload download to solve slow Chinese garbled multiple file downloads and other issues _java

Source: Internet
Author: User
Tags ftp file ftp protocol

Nonsense not to say more, directly to everyone to paste the code, the specific code as follows:

File upload public static Boolean uploadtoftp (String url,int port,string username,string password,string path,string filename
  , InputStream Input) {Boolean success=false;  
    FtpClient ftp=new ftpclient ();//org.apache.commons.net.ftp try{if (port>-1) {ftp.connect (url,port); }else{ftp.connect (URL);//ftp default port is 21}//Many people write with Ftp.getreplycode () to get the return value of the connection, but this will cause Storefilestream to return Nu
      ll if (Ftp.login (Username,password)) {Ftp.enterlocalactivemode ();
      Ftp.setfiletype (Ftpclient.binary_file_type);
      Create the directory, and if present will return the failure ftp.makedirectory (path);
      Switch directory ftp.changeworkingdirectory (path);
      Upload file//ftp protocol stipulates that the file encoding format is iso-8859-1 filename=new String (filename.getbytes ("GBK"), "iso-8859-1");
      OutputStream out=ftp.storefilestream (filename);
      Byte[]bytearray=new byte[4096];
      int read=0;
      while ((Read=input.read (ByteArray))!=-1) {out.write (bytearray,0,read);
      } out.close (); FtP.logout ();
    Sucess=true; 
     The catch (Exception e) {} finally{if (ftp.isconnected ()) {Ftp.disconnecct (); "}}//File download public static Boolean downloadfromftp (String url,int port,string username,string password,string Path,strin
  G LocalPath) {Boolean success=false;
    FtpClient ftp=new ftpclient ();//org.apache.commons.net.ftp try{int reply;  
    if (port>-1) {ftp.connect (url,port);
    }else{ftp.connect (URL);//ftp default port is 21}//Many people write with Ftp.getreplycode () to get the return value of the connection, but this will cause Storefilestream to return null
      Ftp.login (Username,password) Ftp.enterlocalactivemode ();
      Ftp.setfiletype (Ftpclient.binary_file_type);
      Reply=ftp.getreplycode (); if (!
        Ftpreply.ispositioncompletion (Reply)) {ftp.disconnect ();
      Return success;s}//Toggle directory Here you can tell that the failover is not on the FTP above the path ftp.changeworkingdirectory (path);
      Upload file Ftpfile[]fs=ftp.listfiles (); OutputStream ouT=null;
      InputStream In=null;
        for (int i=0;i<fs.length;i++) {ftpfile ff=fs[i];
        String Outfilename=ff.getname ();
        When you create a local file, you turn back the encoding format. String Localfilename=new string (Ff.getname (). GetBytes ("iso-8859-"), "GBK");
        File Localfile=new file (localpath+lcoalfilename);
        Out=new FileOutputStream (LocalFile); 
        In=ftp.retrievefilestream (Outfilename);
        Byte[]bytearray=new byte[4096];
      int read=0;
      while ((Read=in.read (ByteArray))!=-1) {out.write (bytearray,0,read);
      }//This is very important to operate this FTP stream more than once in the channel, to wait for his every command to complete ftp.completependingcommand ();
      Out.flush ();
      Out.close ();
      Ftp.logout ();
  Sucess=true; 
     The catch (Exception e) {} finally{if (ftp.isconnected ()) {Ftp.disconnecct (); }   } }

The above is a small series of Java to introduce the implementation of FTP file upload download to solve the slow Chinese garbled file download and other issues, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.