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