Recently because of the use of FTP upload download, because there are already predecessors used, and then knocked over (absolutely no copy paste Ah, swear). But today suddenly found that ftpclient is not the same, carefully looked at, One is sun.net.ftp.FtpClient, one is org.apache.commons.net.ftp.FTPClient, frighten me hurriedly in the Internet find the difference, carefully look at the difference is not too big, or is not enough careful ah, at the beginning unexpectedly did not find the same, fortunately the late to mend.
First, let's take a look at Sun's ftpclient code:
Public Staticboolean Uploadfilebysun (StringBuffer filecontent,string server,string userName, string userpassword, String path,
String fileName) {ftpclient ftpclient=Newftpclient (); Try { //Open an FTP serverftpclient.openserver (server); //log on with the specified userFtpclient.login (UserName, UserPassword); //go to the specified pathftpclient.cd (path); Telnetoutputstream OS=NULL; //Create a new file//OS = ftpclient.put (New String (Filename.getbytes ("GBK"), "iso-8859-1"));OS =Ftpclient.put (fileName); OutputStreamWriter OSW=Newoutputstreamwriter (OS); BufferedWriter BW=NewBufferedWriter (OSW); Bw.write (Filecontent.tostring ()); Bw.flush (); Bw.close (); } Catch(Exception e) {System. out. println (E.getmessage ()); return false; } finally { Try { //Close FTP ConnectionFtpclient.closeserver (); } Catch(Exception e) {e.printstacktrace (); } } return true; }
As the above code, the upload file is divided into six steps, the first step, open the FTP server, the second step, using the specified user name and password login, the third step, go to the specified file path, the fourth step, create a file, fifth step, write something in the file, and close the file, sixth step, release FTP connection. The final step is to release the FTP connection is important, the general number of FTP server connections are limited, so whether the file upload success or failure must release the connection.
The above example uploads the file is a string literal, it must be mentioned that if the text of the uploaded string is longer (the text uploaded in my project is probably up to 160kb), using the above method may be the case of string loss, unexplained, may be related to the cache, so if the text is longer, We recommend that you use a byte stream. There is also a problem, if the file name to be uploaded is Chinese, the uploaded file name is garbled, garbled problem I try a lot of transcoding can not solve, so I have to use the following method.
Next is the Apache FtpClient code:
Public Staticboolean uploadfilebyapachebybinary (StringBuffer filecontent,string server,string userName, String UserPassword,
String path, String fileName) {ftpclient ftpclient=Newftpclient (); Try{InputStream is=NULL; is=NewBytearrayinputstream (Filecontent.tostring (). GetBytes ()); Ftpclient.connect (server); Ftpclient.login (UserName, UserPassword); Ftpclient.changeworkingdirectory (path); Ftpclient.setfiletype (Ftpclient.binary_file_type); Ftpclient.storefile (NewString (Filename.getbytes ("GBK"),"iso-8859-1") , is); is. Close (); } Catch(Exception e) {e.printstacktrace (); return false; } finally { if(ftpclient.isconnected ()) {Try{ftpclient.disconnect (); } Catch(Exception e) {e.printstacktrace (); } } } return true; }
The procedure for uploading files to Apache is basically consistent with Sun's implementation, except that the method name is somewhat different. Here I convert the string literal into a bytearrayinputstream byte buffer stream, which is a useful thing to do with character-to-stream conversions. Converting to byte uploads will not show the loss of file content.
Ftpclient.storefile (New String (Filename.getbytes ("GBK"), "Iso-8859-1″", is) this code uploads something from the is input stream to the filename file of the FTP server, Here we have the filename of the filename transcoding, tested Chinese is not garbled (the FTP Server uses window, the other platform is not tested), and if we use the sun's FTP implementation, even if the file name for such a similar transcoding, is still garbled.
Two methods we can choose according to their own needs, if using Apache ftpclient, we need to add Commons-net.jar in the project.
Translated from: http://ower22.iteye.com/blog/2126633
The difference between Sun and Apache using FtpClient