Implementing Java FTP client tools with EDTFTPJ

Source: Internet
Author: User
Tags fdf ftp file log log rfc ftp client ftp file transfer

EDTFTPJ is a Java FTP toolkit that is easy to use and feels more usable than Apache, but Apache is more flexible. EDTFTPJ is available in a variety of versions, Java,. NET, and JS. There is a free version for the Java version. I'm using the free version. The rest are commercial versions.

for development, download the free version of the development package first. Home: http://www.enterprisedt.com/Toolkit: Http://www.enterprisedt.com/products/edtftpj/download/edtftpj.zip At the moment, I'm using 2.03. Before developing the FTP client tool, take a look at its e-text development guide: first, development guide 1, in development, the most core API in two classes, respectively, is: filetransferclient  by building this object to be FTP file transfer. Support for "plain" FTP, i.e. RFC 959 ftp ftpclient  functionality and filetransferclient, have been deprecated. Support "plain" ftp, i.e. RFC 959 ftp  2, connect an FTP server  1), build filetransferclient object  filetransferclient FTP = New Filetransferclient ();  2), set remote host and user authentication  ftp.setremotehost (host);  ftp.setusername (username);  ftp.setpassword (password);  ftp.setremoteport (+);  ftp.settimeout (+);  3), log on to the server   Ftp.connect ();  4), close connection  ftp.disconnect (),  3, set more connection Properties  1), set automatic login   Ftp.getadvancedftpsettings (). Setautologin (false);  2), set transfer buffer  ftp.getadvancedsettings (). Settransferbuffersize (1024x768)  3), set the encoding used to get server-side information  ftp.getadvancedsettings (). setcontrolencoding ("GBK") ,  4), set the time interval for transport notifications  ftp.getadvancedsettings (). Settransfernotifyinterval (5000); 5), some other settings   Ftp.getadvancedftpsettings (). Setconnectmode (ftpconnectmode.active);  ftp.getadvancedftpsettings (). SetActiveportrange (61500, 61510);  ftp.getadvancedftpsettings (). Setconnectmode (FTPCONNECTMODE.PASV);  4, Change the current working directory 1), get the current FTP working directory  string directory = ftp.getremotedirectory (), 2), change the current working directory   Ftp.changedirectory (directory), 3), change the current working directory to its parent directory  ftp.changetoparentdirectory ();  5, upload and download, pause 1), upload  ftp.uploadfile (Java.lang.string localfilename, java.lang.String remotefilename) 2), download   Ftp.downloadfile (Java.lang.string localfilename, java.lang.String remotefilename) 3), suspend all transmissions   Ftp.cancelalltransfers ()   Of course, many of these methods have multiple, overloaded formats. A simple encapsulated JAVAFTP client tool is given below:  second, encapsulated client-side tools
Importcom.enterprisedt.net.ftp.FTPException;ImportCom.enterprisedt.net.ftp.FTPFile;Importcom.enterprisedt.net.ftp.FileTransferClient;ImportCom.enterprisedt.net.ftp.WriteMode;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportJava.io.File;Importjava.io.IOException;Importjava.text.ParseException;/*** edtftpj-2.0.3 Tool Simple Package Test * *@authorleizhimin 2008-9-19 13:33:03*/  Public classFtputil {Private StaticLog log = Logfactory.getlog (ftputil.class); PrivateFiletransferclient client =NULL;  Public Static voidMain (string[] args)throwsftpexception, IOException {ftputil ftp=Newftputil ();//ftp.connectserver ();File File =NewFile ("C:\\ooo\\upx"); Uploadlistener Listener=NewUploadlistener (ftp.client);                 Ftp.ftpuploadfolder (file, listener); Ftp.ftpdownload ("C:\\new1111.gif", "/upx/down.gif");         Ftp.disconnect (); }          PublicFtputil () { This. ConnectServer (); }          Publicfiletransferclient getclient () {returnclient; }         /*** Connect to FTP server * *@returnConnection Success Identification*/          Public BooleanConnectServer () {BooleanFlag =false; Client=Newfiletransferclient (); Try{client.setusername ("Admin"); Client.setpassword ("123"); Client.setremotehost ("LocalHost"); Client.setremoteport (21st); Client.settimeout (1200); Client.seteventlistener (Newuploadlistener (client)); Client.getadvancedsettings (). Settransferbuffersize (1000); Client.getadvancedsettings (). Settransfernotifyinterval (5000); Client.getadvancedsettings (). setcontrolencoding ("GBK");                         Client.connect (); Flag=true; } Catch(ftpexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                 returnFlag; }         /*** Close FTP Client connection * *@returnClose Successful Identity*/          Public BooleanDisconnect () {BooleanFlag =false; Try{client.disconnect (); Flag=true; } Catch(ftpexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                 returnFlag; }         /*** Upload file * *@paramFile Files *@paramUL Listener*/          Public voidftpuploadfile (file file, Uploadlistener ul) {String fileName=File.getname (); String FilePath=File.getpath (); Try{client.uploadfile (FilePath, FileName, Writemode.resume); intLen = (int) client.getsize (fileName); Log.info ("Upload file" + FilePath + "complete, size" + Len + "Bytes! "); } Catch(ftpexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }         }         /*** Upload file (clip) * *@paramFile Upload files *@paramUL upload Listener*/          Public voidftpuploadfolder (file file, Uploadlistener ul) {if(File.isfile ()) {ftpuploadfile (file, UL); }                 Try {                         if(File.isdirectory ()) {ftpfile[] ftpfiles=client.directorylist (); BooleanIsexist =false; //iterates through the file name of the FTP current directory, stops the lookup if it exists, and the design is marked as nonexistent if it does not exist                                 for(Ftpfile ftpfile:ftpfiles) {if(Ftpfile.getname (). Equals (File.getname ())) {Isexist=true;  Break; }                                 }                                 //If the directory you want to upload does not exist, create an upload directory                                if(!isexist)                                 {client.createdirectory (File.getname ()); }                                 //Set current directoryclient.changedirectory (File.getname ()); //List of uploaded filesfile[] Upfiles =File.listfiles ();  for(File upfile:upfiles) {if(Upfile.isfile ()) {Ftpuploadfile (upfile, UL); } Else if(Upfile.isdirectory ()) {Ftpuploadfolder (upfile, UL); }} Client.changetoparentdirecto                         Ry (); }                 } Catch(ftpexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(ParseException e) {e.printstacktrace (); }         }         /*** Download File * *@paramlocalfilename Local file name *@paramremotefilename Remote File name *@throwsftpexception *@throwsIOException*/          Public voidFtpdownload (String localfilename, String remotefilename)throwsftpexception, IOException {client.downloadfile (LocalFilename, Remotefilename, writemode.overwrite); } }
ImportCom.enterprisedt.net.ftp.EventAdapter;Importcom.enterprisedt.net.ftp.FileTransferClient;/*** Upload Process monitoring class * *@authorleizhimin 2008-9-22 16:05:53*/ classUploadlistenerextendsEventadapter {Private Longbytestransferred = 0; Privatefiletransferclient ftpclient;  PublicUploadlistener (filetransferclient ftpclient) { This. ftpclient =ftpclient; }          Public voidBytestransferred (String Connid, String remotefilename,Longbytes) {bytestransferred=bytes; } }

Operation Result:

2008-9-22 17:45:48zzvcom.cms.ccm.jmkws.common.FtpUtil ftpuploadfile Info: Upload file C:\ooo\upx\1\12\12. txt completed with a size of 0 bytes! 2008-9-22 17:45:48zzvcom.cms.ccm.jmkws.common.FtpUtil ftpuploadfile Info: Upload file C:\ooo\upx\1\11\11. txt completed with a size of 0 bytes! 2008-9-22 17:45:48zzvcom.cms.ccm.jmkws.common.FtpUtil ftpuploadfile Info: Upload file C:\ooo\upx\1\1. txt completed with a size of 0 bytes! 2008-9-22 17:45:48zzvcom.cms.ccm.jmkws.common.FtpUtil ftpuploadfile Info: Upload file C:\ooo\upx\1\ New FDF DOCUMENT.FDF completed with a size of 0 bytes! 2008-9-22 17:45:48zzvcom.cms.ccm.jmkws.common.FtpUtil ftpuploadfile Info: Upload file C:\ooo\upx\1\ New WinRAR compressed file. rar Complete, size is 20 bytes! 2008-9-22 17:45:48zzvcom.cms.ccm.jmkws.common.FtpUtil ftpuploadfile Info: Upload file C:\ooo\upx\aa.fdf complete, size 0 bytes! Process finished with exit code0

The above code upload supports folder operation.

This article transferred from: http://lavasoft.blog.51cto.com/62575/101108/

Implementing Java FTP client tools with EDTFTPJ

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.