FTP upload Download

Source: Internet
Author: User
Tags ftp login

Package cn.tisson.icsp.utils;


Import Java.io.File;

Import Java.io.FileOutputStream;

Import java.io.IOException;

Import Java.io.InputStream;

Import Java.io.OutputStream;


Import Org.apache.commons.net.ftp.FTP;

Import org.apache.commons.net.ftp.FTPClient;

Import Org.apache.commons.net.ftp.FTPFile;

Import org.apache.commons.net.ftp.FTPReply;


public class Ftpapche {

private static FtpClient ftpclient = new FtpClient ();

private static String encoding = System.getproperty ("file.encoding");


/**

* Description: Uploading files to FTP server

*

* @param URL FTP server hostname

* @param Port FTP Server

* @param username FTP login Account

* @param password FTP login password

* @param path FTP server to save the directory, if the root directory is "/"

* @param filename uploaded to the FTP server

* Input stream @param input local file

* @return returns TRUE, otherwise false

* @Version1.0

*/

public static Boolean uploadfile (string url, int port, string username,

string password, string path, string filename, InputStream input) {

Boolean result = false;


try {

int reply;

If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)

Ftpclient.connect (URL);

Ftp.connect (URL, port);//Connect FTP server

Login

Ftpclient.login (username, password);

ftpclient.setcontrolencoding (encoding);

Verify that the connection is successful

Reply = Ftpclient.getreplycode ();

if (! Ftpreply.ispositivecompletion (Reply)) {

System.out.println ("Connection failed");

Ftpclient.disconnect ();

return result;

}


Transfer working directory to the specified directory

Boolean change = ftpclient.changeworkingdirectory (path);

Ftpclient.setfiletype (Ftp.binary_file_type);

if (change) {

result = Ftpclient.storefile (new String (Filename.getbytes (encoding), "iso-8859-1"), input);

if (result) {

System.out.println ("Upload succeeded!");

}

}

Input.close ();

Ftpclient.logout ();

} catch (IOException e) {

E.printstacktrace ();

} finally {

if (ftpclient.isconnected ()) {

try {

Ftpclient.disconnect ();

} catch (IOException IoE) {

}

}

}

return result;

}


/**

* Description: Download files from FTP server

*

* @param URL FTP server hostname

* @param Port FTP Server

* @param username FTP login Account

* @param password FTP login password

* @param the relative path on the RemotePath FTP server

* @param filename to download

* @param localPath saved to local path after download

* @return

* @Version1.0

*/

public static Boolean downfile (string url, int port, string username,

string password, string remotepath, String fileName,

String LocalPath) {

Boolean result = false;

try {

int reply;

ftpclient.setcontrolencoding (encoding);


/*

* In order to upload and download Chinese documents, some places suggest using the following two sentences instead

* New String (remotepath.getbytes (encoding), "Iso-8859-1") transcoding.

* After testing, pass.

*/

Ftpclientconfig conf = new Ftpclientconfig (FTPCLIENTCONFIG.SYST_NT);

Conf.setserverlanguagecode ("zh");


Ftpclient.connect (URL, port);

If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)

Ftpclient.login (username, password);//Login

Set file Transfer type to binary

Ftpclient.setfiletype (Ftpclient.binary_file_type);

Get the FTP Login answer code

Reply = Ftpclient.getreplycode ();

Verify that the login is successful

if (! Ftpreply.ispositivecompletion (Reply)) {

Ftpclient.disconnect ();

SYSTEM.ERR.PRINTLN ("FTP server refused connection.");

return result;

}

Transfer to FTP server directory to the specified directory

Ftpclient.changeworkingdirectory (New String (Remotepath.getbytes (encoding), "iso-8859-1"));

Get a list of files

ftpfile[] fs = Ftpclient.listfiles ();

for (Ftpfile Ff:fs) {

if (Ff.getname (). Equals (FileName)) {

File LocalFile = new file (LocalPath + "/" + ff.getname ());

OutputStream is = new FileOutputStream (localfile);

Ftpclient.retrievefile (Ff.getname (), is);

Is.close ();

}

}


Ftpclient.logout ();

result = true;

} catch (IOException e) {

E.printstacktrace ();

} finally {

if (ftpclient.isconnected ()) {

try {

Ftpclient.disconnect ();

} catch (IOException IoE) {

}

}

}

return result;

}

}


FTP upload Download

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.