Recently in the ftp file upload and download, based on this, collated a bit of information. Originally wanted to adopt the method of Java, but looked at the jdk1.6 and 1.7 of the implementation of a little difference, so the use of Apache under the framework of the implementation of ...
1. Refer to 3 Packages first
2. Then the code for the related class
/*** FTP Link Constants **/ Public classFTP {PrivateString ipaddr;//IP Address PrivateInteger Port;//Port number PrivateString UserName;//User name PrivateString pwd;//Password PrivateString path;//AAA Path PublicString getipaddr () {returnipaddr; } Public voidsetipaddr (String ipaddr) { This. ipaddr =ipaddr; } PublicInteger Getport () {returnPort; } Public voidSetport (Integer port) { This. Port =Port; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString getpwd () {returnpwd; } Public voidsetpwd (String pwd) { This. PWD =pwd; } PublicString GetPath () {returnpath; } Public voidSetPath (String path) { This. Path =path; } }
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;Importorg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPFile;Importorg.apache.commons.net.ftp.FTPReply;ImportOrg.apache.log4j.Logger; Public classFtputil {Private StaticLogger Logger=logger.getlogger (Ftputil.class); Private Staticftpclient FTP; /*** Get FTP connection *@paramF *@return * @throwsException*/ Public Static BooleanConnectftp (FTP f)throwsexception{FTP=Newftpclient (); Booleanflag=false; intreply; if(F.getport () = =NULL) {Ftp.connect (F.getipaddr (),21st); }Else{ftp.connect (F.getipaddr (), F.getport ()); } ftp.login (F.getusername (), f.getpwd ()); Ftp.setfiletype (Ftpclient.binary_file_type); Reply=Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnFlag; } ftp.changeworkingdirectory (F.getpath ()); Flag=true; returnFlag; } /*** Close FTP connection*/ Public Static voidcloseftp () {if(ftp!=NULL&&ftp.isconnected ()) { Try{ftp.logout (); Ftp.disconnect (); } Catch(IOException e) {e.printstacktrace (); } } } /*** FTP upload file *@paramF *@throwsException*/ Public Static voidUpload (File f)throwsexception{if(F.isdirectory ()) {ftp.makedirectory (F.getname ()); Ftp.changeworkingdirectory (F.getname ()); string[] Files=f.list (); for(String fstr:files) {File file1=NewFile (F.getpath () + "/" +fstr); if(File1.isdirectory ()) {upload (file1); Ftp.changetoparentdirectory (); }Else{File file2=NewFile (F.getpath () + "/" +fstr); FileInputStream input=NewFileInputStream (file2); Ftp.storefile (File2.getname (), input); Input.close (); } } }Else{File file2=NewFile (F.getpath ()); FileInputStream input=NewFileInputStream (file2); Ftp.storefile (File2.getname (), input); Input.close (); } } /*** Download LINK configuration *@paramF *@paramlocalbasedir Local Directory *@paramremotebasedir Remote Directory *@throwsException*/ Public Static voidStartdown (FTP f,string localbasedir,string remotebasedir)throwsexception{if(Ftputil.connectftp (f)) {Try{ftpfile[] files=NULL; BooleanChangedir =ftp.changeworkingdirectory (Remotebasedir); if(Changedir) {ftp.setcontrolencoding ("GBK"); Files=Ftp.listfiles (); for(inti = 0; i < files.length; i++) { Try{downloadFile (files[i], Localbasedir, Remotebasedir); }Catch(Exception e) {logger.error (e); Logger.error ("<" +files[i].getname () + "> download Failed"); } } } } Catch(Exception e) {logger.error (e); Logger.error ("Exception occurred during download"); } }Else{logger.error ("Link failed! "); } } /*** * Download FTP file * When you need to download FTP file, call this method * based on <b> get filename, local address, remote address </b> download * * @paramFtpfile *@paramRelativelocalpath *@paramRelativeremotepath*/ Private Static voidDownloadFile (ftpfile ftpfile, String relativelocalpath,string relativeremotepath) {if(Ftpfile.isfile ()) {if(Ftpfile.getname (). IndexOf ("?") = =-1) {OutputStream OutputStream=NULL; Try{File Locafile=NewFile (relativelocalpath+ftpfile.getname ()); //determine if the file exists, and then return if(Locafile.exists ()) {return; }Else{OutputStream=NewFileOutputStream (relativelocalpath+ftpfile.getname ()); Ftp.retrievefile (Ftpfile.getname (), outputstream); Outputstream.flush (); Outputstream.close (); } } Catch(Exception e) {logger.error (e); } finally { Try { if(OutputStream! =NULL) {outputstream.close (); } } Catch(IOException e) {logger.error ("Output file Stream exception"); } } } } Else{String Newlocalrelatepath= Relativelocalpath +Ftpfile.getname (); String Newremote=NewString (relativeremotepath+ftpfile.getname (). toString ()); File FL=NewFile (Newlocalrelatepath); if(!fl.exists ()) {Fl.mkdirs (); } Try{Newlocalrelatepath= Newlocalrelatepath + '/'; Newremote= Newremote + "/"; String Currentworkdir=ftpfile.getname (). toString (); BooleanChangedir =ftp.changeworkingdirectory (Currentworkdir); if(Changedir) {ftpfile[] files=NULL; Files=Ftp.listfiles (); for(inti = 0; i < files.length; i++) {downloadFile (files[i], Newlocalrelatepath, newremote); } } if(Changedir) {ftp.changetoparentdirectory (); } } Catch(Exception e) {logger.error (e); } } } Public Static voidMain (string[] args)throwsexception{FTP F=NewFTP (); F.SETIPADDR ("1111"); F.setusername ("Root"); F.setpwd ("111111"); Ftputil.connectftp (f); File File=NewFile ("F:/test/com/test/testng.java"); Ftputil.upload (file);//upload the file to FTPFtputil.startdown (F, "e:/", "/xxtest");//Download the ftp file TestSystem.out.println ("OK"); } }
The above code is tested and passed ...
Project and related package download: Http://pan.baidu.com/s/1hq5p7NI
Article Source: http://www.cnblogs.com/huzi007/p/4236150.html
Java implementation of FTP file upload and download