FTP implementation of large file upload download

Source: Internet
Author: User
Tags ftp connection ftp client
Java implementation ftp upload download functionJava FTP client Toolkit for many, here I choose Apache's ftpclient. The acquisition of this package can be obtained by [Url]http://commons.apache.org/net/[/url], and I am using the latest commons-net-1.4.1.zip. It contains a number of Java Network Programming Toolkit, the official documentation listed below:
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 very useful.   Here I use some of the FTP-related packages. Before you write your program, configure a simple FTP server, as shown in the following figure:
Here is a use of ftpclient to achieve the FTP upload and download functions, mainly for this package has a perceptual understanding. The routine is as follows: Import org.apache.commons.io.IOUtils;
Import org.apache.commons.net.ftp.FTPClient;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.FileOutputStream;

/**
* Apache Commons-net Try one to see what the FTP client tools do
*
* @author: leizhimin,2008-8-20 14:00:38. <p>
*/
public class Ftptest {
public static void Main (string[] args) {
Testupload ();
Testdownload ();
}

/**
* FTP upload a single file test
*/
public static void Testupload () {
FtpClient ftpclient = new FtpClient ();
FileInputStream FIS = null;

try {
Ftpclient.connect ("192.168.14.117");
Ftpclient.login ("admin", "123");

File Srcfile = new file ("C:\\new.gif");
FIS = new FileInputStream (srcfile);
Set up upload Directory
Ftpclient.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 new RuntimeException ("FTP client error. ", e);
finally {
ioutils.closequietly (FIS);
try {
Ftpclient.disconnect ();
catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException ("Shutdown FTP connection) exception occurred." ", e);
}
}
}

/**
* FTP Download a single file test
*/
public static void Testdownload () {
FtpClient ftpclient = new FtpClient ();
FileOutputStream fos = null;

try {
Ftpclient.connect ("192.168.14.117");
Ftpclient.login ("admin", "123");

String remotefilename = "/admin/pic/3.gif";
FOS = new FileOutputStream ("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 new RuntimeException ("FTP client error. ", e);
finally {
ioutils.closequietly (FOS);
try {
Ftpclient.disconnect ();
catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException ("Shutdown FTP connection) exception occurred." ", e);
}
}
}
}
  test results show that file upload and download are successful.   feel that Apache is doing a good job with this.

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.