Java implementation of FTP file upload download, solve slow, Chinese garbled, a number of file downloads and other issues

Source: Internet
Author: User
Tags ftp file ftp protocol

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 The default port is 21}//A lot of people write the return value of the connection using Ftp.getreplycode (), but this causes the Storefilestream to return NULL if    (Ftp.login (Username,password))      {Ftp.enterlocalactivemode ();      Ftp.setfiletype (Ftpclient.binary_file_type);      Creates a directory that returns a failure ftp.makedirectory (path) if it exists;      Switch directory ftp.changeworkingdirectory (path);      Upload file//ftp protocol specifies 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; }} 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,string L  Ocalpath) {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}//A lot of people write the return value of the connection using Ftp.getreplycode (), but this causes the storefilestream to return null ft      P.login (Username,password) Ftp.enterlocalactivemode ();      Ftp.setfiletype (Ftpclient.binary_file_type);      Reply=ftp.getreplycode (); if (!        Ftpreply.ispositioncompletion (Reply)) {ftp.disconnect ();      Return success;s}//switch directory here can be judged, the failure of the switch indicates that FTP does not have this 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 creating a local file, turn the encoding format back to 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 sentence is important to operate the channel of this FTP stream multiple times, to wait for his every command to complete ftp.completependingcommand ();      Out.flush ();      Out.close ();      Ftp.logout ();  Sucess=true;      } catch (Exception e) {} finally{if (ftp.isconnected ()) {Ftp.disconnecct (); }}} The above code in the blog Park Editor hand may be a bit wrong, the above two ways faster than uploading with storefile download with Retrievefile This method is much faster

  

Java implementation of FTP file upload download, solve slow, Chinese garbled, a number of file downloads and other issues

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.