Java ftpclient implementation file upload download _java

Source: Internet
Author: User
Tags ftp login server port

In Java programs, often need to deal with FTP, such as uploading files to the FTP server, download files, this article briefly describes how to use the FtpClient in Jakarta Commons (in the Commons-net package) to achieve upload download files. The jar packages used by
are:  
commons-net-1.4.1.jar 
jakarta-oro.jar 
First, upload file

 File Upload source code/** * Description: Upload file to FTP server * @Version1.0 * @param URL FTP server hostname * @param port FTP Service Server port * @param username FTP login account * @param password FTP login password * @param path FTP server save directory * @param filename uploaded to FTP The file name on the server * @param input stream * @return successfully returns TRUE, otherwise returns false */public static Boolean UploadFile (String u RL,//FTP server hostname int PORT,//FTP Server port string username,//FTP login account string password,//ftp login password string Path,//ftp server saves directory String filename,//File name uploaded to FTP server InputStream input//input stream) {Boolean success = Fal 
   Se 
   ftpclient ftp = new FtpClient (); 
    try {int reply; Ftp.connect (URL, port);//Connect FTP server//If the default port is used, you can connect the FTP server directly using the Ftp.connect (URL) ftp.login (username, password); 
    /Login reply = Ftp.getreplycode (); if (! 
     Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); 
    return success; 
    } ftp.changeworkingdirectory (path); Ftp.storefiLe (filename, input); 
    Input.close (); 
    Ftp.logout (); 
   Success = true; 
   catch (IOException e) {e.printstacktrace (); 
     finally {if (ftp.isconnected ()) {try {ftp.disconnect (); 
  catch (IOException IoE) {}} return success;  }

The following is a file upload test case:

 /** 
  * Upload the local file to the FTP server 
  * 
 /public void Testuploadfromdisk () { 
  try { 
   FileInputStream in=new FileInputStream (New File ("D:/test.txt")); 
   Boolean flag = UploadFile ("127.0.0.1", "Administrator", "zyuc2011", "Test", "test.txt", in); 
   SYSTEM.OUT.PRINTLN (flag); 
  } catch (FileNotFoundException e) { 
   e.printstacktrace (); 
  }  
 } 
 /** 
  * Generates a file on the FTP server and writes a string to the file 
  * */public 
 void testuploadfromstring () { 
  try { 
   String str = "This is the string to write!" "; 
   InputStream input = new Bytearrayinputstream (str.getbytes ("Utf-8")); 
   Boolean flag = UploadFile ("127.0.0.1", "Administrator", "zyuc2011", "Test", "test.txt", input); 
   SYSTEM.OUT.PRINTLN (flag); 
  } catch (Unsupportedencodingexception e) { 
   e.printstacktrace (); 
  } 
 } 

Second, File download
File Download Source code

 /** * 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 FTP Server relative path * @param filename to download * @param LocalPath Save to Local path * @return/public static Boolean downfile (String URL,//ftp server hostname int port, FTP Server port string username,//ftp login account string password,//ftp login password string remotepath,//ftp relative path string F on server 
  ilename,//to download the filename String localpath//saved to the local path after downloading) {Boolean success = false; 
  ftpclient ftp = new FtpClient (); 
   try {int reply; 
   Ftp.connect (URL, port); 
   If you use the default port, you can use the Ftp.connect (URL) way to connect directly to the FTP server Ftp.login (username, password);/Login reply = Ftp.getreplycode (); if (! 
    Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); 
   return success; 
   } ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles (); for (Ftpfile Ff:fs{if (Ff.getname (). Equals (FileName)) {File LocalFile = new File (localpath+ "/" +ff.getname ());  
     OutputStream is = new FileOutputStream (localfile); 
     Ftp.retrievefile (Ff.getname (), is); 
    Is.close (); 
   } ftp.logout (); 
  Success = true; 
  catch (IOException e) {e.printstacktrace (); 
    finally {if (ftp.isconnected ()) {try {ftp.disconnect (); 
 catch (IOException IoE) {}} return success; 
 }

The following are test cases for file downloads:

 /** 
  * Download files on FTP server to local 
  */ 
  public 
 void Testdownfile () { 
  try { 
   Boolean flag = Downfile (" 127.0.0.1 ",", "Administrator", "zyuc2011", "Test", "Test.txt", "d:/"); 
   SYSTEM.OUT.PRINTLN (flag); 
  } catch (Exception e) { 
   e.printstacktrace (); 
  }   
 

The above is the entire content of this article, I hope to help you learn.

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.