Java implementation of FTP upload and download function

Source: Internet
Author: User
Tags ftp connection ftp client

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

Related Article

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.