Java Implementation ftp download file

Source: Internet
Author: User
Tags ftp protocol

FTP upload download file, is to follow the FTP protocol upload downloaded files, this example only download the file as an example.

Important ways to explain:

1.FTP function dependent path: org.apache.commons.net.ftp.*;

2.ftp default port is 21, if non-default port connection, specify: Ftp.connect (FTPHOSTADDR, 22);//22 as port number

3.ftp.changeworkingdirectory (Ftppath)//Implement switch directory

4.ftpfile[] fs = Ftp.listfiles (); Gets the list of files in the specified directory

public class Ftptools {private final static string ftphostaddr = "xxx";//server address private final static string Ftppath = "XXX";//Operating server directory private final static string ftpname = "xxx";//server login private final static string ftppwd = "xxx";    /Login Password Private final static String LocalPath = Getcurentcontentpath () + "uploadfiles";        Private final static String Fileseparator = System.getproperty ("File.separator");    private static final Logger Logger = Logger.getlogger (Ftptools.class); public static void Main (string[] args) {ftptools tools = new Ftptools (); Tools.downfile ("test.txt");}  /** * Download files from File server to local * @param filename */public static void Downfile (String filename) {ftpclient ftp = initftp ();              try{//4. Specify the directory to download ftp.changeworkingdirectory (ftppath);//Transfer to the FTP server directory//5. Traverse the Downloaded directory              ftpfile[] fs = Ftp.listfiles (); for (Ftpfile ff:fs) {//Solve Chinese garbled problem, two decoding byte[] Bytes=ff.getname (). getBytes("Iso-8859-1");                String fn=new string (bytes, "UTF8"); if (fn.equals (filename)) {//6. write operation, write it to a local file Logger.info ("Download file:" +filename+ "Start:" +                    System.currenttimemillis ());                      File LocalFile = new file (LocalPath +fileseparator+ ff.getname ());                      OutputStream is = new FileOutputStream (localfile);                                        Ftp.retrievefile (Ff.getname (), is); 7. Determine if the local file is written correctly, and if it is written correctly, delete the file on the file server if (Localfile.exists ()) {logger.info ("Delete file on server:"                    +filename);                    Ftp.deletefile (Ff.getname ());                    } logger.info ("Download file:" +filename+ "End:" +system.currenttimemillis ());                  Is.close ();   }} ftp.logout (); }catch (Exception e) {e.printstacktrace (); New Exception ("error occurred while downloading files from server");} finally{if (ftp.isconnected ()) {TRy {ftp.disconnect ();                } catch (IOException IoE) {ioe.printstacktrace (); }}}} public static FtpClient initftp () {int reply; ftpclient ftp = new ftpclient (); try {//1. Connect server//ftp.connect (FTPHOSTADDR); Ftp.connect (Ftphostaddr, 22);//2. Log on to the server If using the default port, you can use Ftp.connect (URL) to connect directly to the FTP server Ftp.login (ftpname, ftppwd);//3. Determine whether the login succeeded reply = Ftp.getreplycode (); if (! Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();}} catch (Exception e) {e.printstacktrace (); New Exception ("Server Connection Failed");}  return FTP;} public static string Getcurentcontentpath () {String path = ""; Path = FtpTools.class.getClassLoader (). GetResource (""). ToString (); Path = Path.replace ("File:", ""). Substring (0, Path.indexof ("Web-inf")). Replace ("web-in", ""). Replace ("Web-i", "" "); return path; }}

  

Java Implementation ftp download file

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.