Java implementation of FTP file upload and download the function of the instance code _java

Source: Internet
Author: User
Tags file upload ftp file ftp login file transfer protocol

FTP is the English abbreviation for file Transfer Protocol (document Transfer Protocol), while Chinese is called "the Protocol". A two-way transmission of control files for use on the Internet. At the same time, it is also an application (application). There are different FTP applications based on different operating systems, and all of these applications adhere to the same protocol to transfer files. In the use of FTP, users often encounter two concepts: "Download" (Download) and "Upload" (Upload). A "Download" file is a copy of a file from a remote host to your computer, and an "upload" file is a file that copies files from your computer to a remote host. In Internet language, users can upload (download) files to remote hosts (from) through the client program.

First downloaded the serv-u to set up their own computer for FTP file server, easy to operate.

1.FTP Downloads of files (downloaded from FTP server to native)

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
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.GETP
Roperty ("file.encoding");  /** * Description: Download files from FTP server * * @Version1.0 * @param URL * FTP server hostname * @param port * FTP server PORTS * @param username * FTP login account * @param password * FTP login password * @param remotepath * The relative path on the FTP server * @param filename * The filename to download * @param localpath * Save to Local path after download * @return/public static Boolean downfile (string url, int port, string username, string password, string remo Tepath, String fileName, String localpath) {Boolean result = false; try {int reply; Ftpclient.setcontrolencoding (encoding);
* * to upload and download Chinese files, there are suggestions to use the following two sentences instead of the * New String (remotepath.getbytes (encoding), "Iso-8859-1") transcoding.
* After testing, pass.
*///Ftpclientconfig conf = new Ftpclientconfig (FTPCLIENTCONFIG.SYST_NT);
Conf.setserverlanguagecode ("en");
Ftpclient.connect (URL, port);
If you use the default port, you can connect to the FTP server directly using the Ftp.connect (URL) ftpclient.login (username, password);//Login//settings file Transfer type is 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 the FTP server directory to the specified directory Ftpclient.changeworkingdirectory (new String (Remotepath.getbytes (encoding), "iso-8859-1"));
Get file list 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; /** * Download files on FTP server to local */public void Testdownfile () {try {Boolean flag = Downfile ("10.0.0.102", "admin", "12345
6 ","/"," Ip.txt "," e:/");
SYSTEM.OUT.PRINTLN (flag);
catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {Ftpapche fa = new Ftpapche (); Fa.testdownfile ();}}

2.FTP File upload (upload from native to FTP server)

Import Java.io.File;
Import Java.io.FileInputStream;
Import org.apache.commons.net.ftp.FTPClient;
Import org.apache.commons.net.ftp.FTPReply;  
public class Ftptest_05 {private ftpclient ftp;/** * * @param path upload to FTP server @param addr Address * @param Port port number * @param username username * @param password Password * @return * @throws Exception/private Boolean connect (String path,str ing addr,int port,string username,string password) throws Exception {Boolean result = false; ftp = new ftpclient (); int R
eply;
Ftp.connect (Addr,port);
Ftp.login (Username,password);
Ftp.setfiletype (Ftpclient.binary_file_type);
Reply = Ftp.getreplycode (); if (!
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); return result;} ftp.changeworkingdirectory (path);
result = true;
return result; /** * * @param files or folders uploaded by file * @throws Exception/private void upload (file file) throws exception{if (file.isdir
Ectory ()) {ftp.makedirectory (File.getname ()); Ftp.changeworkingdirectory (File.getname ()); String[] files = file.list (); for (int i = 0;i < files.length;i++) {File file1 = new File (File.getpath () + "\" +files[i]); if (File1.isdirectory ()) {u
Pload (FILE1);
Ftp.changetoparentdirectory ();
}else{file File2 = new file (File.getpath () + "\" +files[i));
FileInputStream input = new FileInputStream (file2);
Ftp.storefile (File2.getname (), input);
Input.close ();
}}else{File File2 = new file (File.getpath ());
FileInputStream input = new FileInputStream (file2);
Ftp.storefile (File2.getname (), input);
Input.close (); } public static void Main (string[] args) throws exception{ftptest_05 t = new ftptest_05 (); Boolean connflag = T.connect
("/", "10.0.0.105", "ls", "123456");
System.out.println ("Connflag:" + connflag);
File File = new file ("d:\\test02");//The address of the native System.out.println ("file:" + file);
T.upload (file); System.out.println ("Upload:" + "OK");}

The above is a small series to introduce the Java implementation of FTP file upload and download the example code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.