Java upload download file via FTP server _java

Source: Internet
Author: User
Tags ftp login rar filezilla server port

Using an FTP server is a good solution for applications that use files for exchanging data.

For more detailed configuration procedures for FileZilla server servers, see the FileZilla Server Installation configuration tutorial. Before a friend said, upload large files (more than hundreds of M files) to the FTP server will reproduce the problem can not be renamed, but I test upload 2G file to FileZilla server does not have the problem, friends can be assured to use the code.

Favftputil.java

Package com.favccxx.favsoft.util;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
 
Import Java.io.OutputStream;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile;
 
Import org.apache.commons.net.ftp.FTPReply; public class Favftputil {/** * upload file (available for Action/controller layer) * @param hostname FTP server address * @param Port FTP Service Service port number * @param username FTP login account * @param password FTP login password * @param pathname FTP Server save directory * @param fileName File name after uploading to FTP server * @param inputstream input file stream * @return/public static Boolean uploadfile (String hostname, int po RT, string Username, string password, string pathname, String fileName, InputStream InputStream) {Boolean flag = False
    ;
    FtpClient ftpclient = new FtpClient ();
    Ftpclient.setcontrolencoding ("UTF-8");
      try {//Connect FTP server Ftpclient.connect (hostname, port); Login ftP Server Ftpclient.login (username, password);
      Whether to successfully log on to the FTP server int replycode = Ftpclient.getreplycode (); if (!
      Ftpreply.ispositivecompletion (Replycode)) {return flag;
      } ftpclient.setfiletype (Ftpclient.binary_file_type);
      Ftpclient.makedirectory (pathname);
      Ftpclient.changeworkingdirectory (pathname);
      Ftpclient.storefile (FileName, InputStream);
      Inputstream.close ();
      Ftpclient.logout ();
    Flag = true;
    catch (Exception e) {e.printstacktrace ();
        } finally{if (ftpclient.isconnected ()) {try {ftpclient.disconnect ();
        catch (IOException e) {e.printstacktrace ();
  }} return flag; /** * Upload file (can rename file) * @param hostname FTP server address * @param port FTP Server port number * @param username FTP Login Account * @param password FTP login password * @param pathname FTP Server save directory * @param filename uploaded to the FTP server after the file name * @param origin FileName to upload file name (Absolute address) * @return/public static Boolean uploadfilefromproduction (string hostname, int port, string username, Stri
    ng password, string pathname, string filename, String originfilename) {Boolean flag = false;
      try {inputstream InputStream = new FileInputStream (new File (Originfilename));
    Flag = UploadFile (hostname, port, username, password, pathname, filename, inputstream);
    catch (Exception e) {e.printstacktrace ();
  return flag; /** * Upload file (no file renaming operation) * @param hostname FTP server address * @param port FTP Server port number * @param username FTP Login
   Account * @param password FTP login password * @param pathname FTP Server save directory * @param originfilename the name of the file to be uploaded (absolute address) * @return */public static Boolean uploadfilefromproduction (string hostname, int port, string username, string password, string
    Pathname, String originfilename) {Boolean flag = false;
      try {String fileName = new File (originfilename). GetName (); InputStream InpuTstream = new FileInputStream (new File (Originfilename));
    Flag = UploadFile (hostname, port, username, password, pathname, fileName, InputStream);
    catch (Exception e) {e.printstacktrace ();
  return flag; /** * Delete file * @param hostname FTP server address * @param port FTP Server port number * @param username FTP login Account * @pa RAM Password FTP login password * @param pathname FTP Server save directory * @param filename name of the file to be deleted * @return/public static B Oolean DeleteFile (string hostname, int port, string username, string password, string pathname, string filename) {bool
    EAN flag = false;
    FtpClient ftpclient = new FtpClient ();
      try {//Connect FTP server Ftpclient.connect (hostname, port);
      Login to FTP server Ftpclient.login (username, password);
      Verify that the FTP server is logged on successfully int replycode = Ftpclient.getreplycode (); if (!
      Ftpreply.ispositivecompletion (Replycode)) {return flag; }//Toggle FTP directory Ftpclient.changeworkingdirectory (PathnaME);
      Ftpclient.dele (filename);
      Ftpclient.logout ();
    Flag = true;
    catch (Exception e) {e.printstacktrace ();
        } finally{if (ftpclient.isconnected ()) {try {ftpclient.logout ();
  The catch (IOException e) {}}} return flag; /** * Download file * @param hostname FTP server address * @param port FTP Server port number * @param username FTP login Account * @param
   Password FTP login password * @param pathname FTP Server file directory * @param filename File name * @param localpath file path after download * @return */public static Boolean DownloadFile (string hostname, int port, string username, string password, string pathname, S
    Tring filename, String localpath) {Boolean flag = false;
    FtpClient ftpclient = new FtpClient ();
      try {//Connect FTP server Ftpclient.connect (hostname, port);
      Login to FTP server Ftpclient.login (username, password);
      Verify that the FTP server is logged on successfully int replycode = Ftpclient.getreplycode (); if (! FtpReply.ispositivecompletion (Replycode)) {return flag;
      }//Toggle FTP directory Ftpclient.changeworkingdirectory (pathname);
      ftpfile[] Ftpfiles = Ftpclient.listfiles (); for (Ftpfile file:ftpfiles) {if (Filename.equalsignorecase (File.getname ())) {File LocalFile = new file (l
          Ocalpath + "/" + file.getname ());
          OutputStream OS = new FileOutputStream (localfile);
          Ftpclient.retrievefile (File.getname (), OS);
        Os.close ();
      } ftpclient.logout ();
    Flag = true;
    catch (Exception e) {e.printstacktrace ();
        } finally{if (ftpclient.isconnected ()) {try {ftpclient.logout ();
  The catch (IOException e) {}}} return flag; }
 
}

Favftputiltest.java

Package com.favccxx.favsoft.util;
 
Import Junit.framework.TestCase;
 
public class Favftptest extends TestCase {public
   
  void Testfavftputil () {
    String hostname = "127.0.0.1";
    int port =;
    String username = "Business";
    String Password = "Business";
    String pathname = "Business/ebook"; 
    String filename = "Big.rar"; 
    String originfilename = "C:\\users\\downloads\\downloads.rar";
    Favftputil.uploadfilefromproduction (hostname, port, username, password, pathname, filename, originfilename);   String localpath = "d:/";   Favftputil.downloadfile (hostname, port, username, password, pathname, filename, localpath);
  }
 

The above is the entire content of this article, I hope you like.

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.