Java FTP Client Toolkit Many, in this I choose the Apache ftpclient. This package can be obtained by http://commons.apache.org/net/and I am using the latest commons-net-1.4.1.zip. It contains a number of Java Network Programming Toolkit, the official documents are listed as follows:1, the Support network protocol is as follows:
FTP, NNTP, SMTP, POP3, Telnet, TFTP, Finger, Whois, rexec/rcmd/rlogin, Time (rdate) and daytime, Echo, Discard, NTP/SNTP
are useful, here I use some of the FTP-related packages. 2. Configuring the FTP serverbefore you write a program, configure a simple FTP server, such as:here is a use of ftpclient to achieve the FTP upload and download functions, mainly for this package has a perceptual understanding. The routines are as follows:
Importorg.apache.commons.io.IOUtils;Importorg.apache.commons.net.ftp.FTPClient;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.FileOutputStream;/*** Apache Commons-net Try it out and see how the FTP client tool does it well * *@author: leizhimin,2008-8-20 14:00:38. <p>*/ Public classFtptest { Public Static voidMain (string[] args) {testupload (); Testdownload (); } /*** FTP upload single file test*/ Public Static voidtestupload () {ftpclient ftpclient=Newftpclient (); FileInputStream FIS=NULL; Try{ftpclient.connect ("192.168.14.117"); Ftpclient.login ("Admin", "123"); File Srcfile=NewFile ("C:\\new.gif"); FIS=NewFileInputStream (srcfile); //Set upload directoryFtpclient.changeworkingdirectory ("/admin/pic"); Ftpclient.setbuffersize (1024); Ftpclient.setcontrolencoding ("GBK"); //set File type (binary)Ftpclient.setfiletype (Ftpclient.binary_file_type); Ftpclient.storefile ("3.gif", FIS); } Catch(IOException e) {e.printstacktrace (); Throw NewRuntimeException ("FTP Client Error! ", E); } finally{ioutils.closequietly (FIS); Try{ftpclient.disconnect (); } Catch(IOException e) {e.printstacktrace (); Throw NewRuntimeException ("Close FTP connection exception occurred! ", E); } } } /*** FTP Download single file test*/ Public Static voidtestdownload () {ftpclient ftpclient=Newftpclient (); FileOutputStream Fos=NULL; Try{ftpclient.connect ("192.168.14.117"); Ftpclient.login ("Admin", "123"); String Remotefilename= "/admin/pic/3.gif"; FOS=NewFileOutputStream ("C:/down.gif"); Ftpclient.setbuffersize (1024); //set File type (binary)Ftpclient.setfiletype (Ftpclient.binary_file_type); Ftpclient.retrievefile (Remotefilename, FOS); } Catch(IOException e) {e.printstacktrace (); Throw NewRuntimeException ("FTP Client Error! ", E); } finally{ioutils.closequietly (FOS); Try{ftpclient.disconnect (); } Catch(IOException e) {e.printstacktrace (); Throw NewRuntimeException ("Close FTP connection exception occurred! ", E); } } } }
The test results show that the file upload and download are successful!
This article transferred from: http://lavasoft.blog.51cto.com/62575/93883/
Java implementation of FTP upload and download function