Java method of uploading and downloading files via FTP server

  • 2020-04-01 04:04:03
  • OfStack

Using an FTP server is a great solution for applications that use files to exchange data.

See the FileZilla Server installation and configuration tutorial for details on the setup and configuration of the FileZilla Server Server. Some friends said before that uploading large files (hundreds of M or more files) to the FTP Server will reproduce the problem of unable to rename, but I personally tested the upload of 2G files to FileZilla Server did not have this problem, friends can be assured to use this 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 Action/Controller Layer use) 
   * @param hostname FTP Server address 
   * @param port  FTP Server port number 
   * @param username  FTP Login account 
   * @param password  FTP The login password 
   * @param pathname  FTP Server save directory 
   * @param fileName   Uploaded to the FTP The name of the file after the server 
   * @param inputStream  Input file stream 
   * @return
   */
  public static boolean uploadFile(String hostname, int port, String username, String password, String pathname, String fileName, InputStream inputStream){
    boolean flag = false;
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("UTF-8");
    try {
      //Connect to FTP server
      ftpClient.connect(hostname, port);
      //Login to FTP server
      ftpClient.login(username, password);
      // The success of Login to 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;
  }
   
   
  
  public static boolean uploadFileFromProduction(String hostname, int port, String username, String 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;
  }
   
  
  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;
  }
   
   
  
  public static boolean deleteFile(String hostname, int port, String username, String password, String pathname, String filename){
    boolean flag = false;
    FTPClient ftpClient = new FTPClient();
    try {
      //Connect to FTP server
      ftpClient.connect(hostname, port);
      //Login to FTP server
      ftpClient.login(username, password);
      //Verify that the FTP server is logged in successfully
      int replyCode = ftpClient.getReplyCode();
      if(!FTPReply.isPositiveCompletion(replyCode)){
        return flag;
      }
      //FTP directory
      ftpClient.changeWorkingDirectory(pathname);
      ftpClient.dele(filename);
      ftpClient.logout();
      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
    } finally{
      if(ftpClient.isConnected()){
        try {
          ftpClient.logout();
        } catch (IOException e) {
         
        }
      }
    }
    return flag;
  }
   
  
  public static boolean downloadFile(String hostname, int port, String username, String password, String pathname, String filename, String localpath){
    boolean flag = false;
    FTPClient ftpClient = new FTPClient();
    try {
      //Connect to FTP server
      ftpClient.connect(hostname, port);
      //Login to FTP server
      ftpClient.login(username, password);
      //Verify that the FTP server is logged in successfully
      int replyCode = ftpClient.getReplyCode();
      if(!FTPReply.isPositiveCompletion(replyCode)){
        return flag;
      }
      //FTP directory
      ftpClient.changeWorkingDirectory(pathname);
      FTPFile[] ftpFiles = ftpClient.listFiles();
      for(FTPFile file : ftpFiles){
        if(filename.equalsIgnoreCase(file.getName())){
          File localFile = new File(localpath + "/" + 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();
        } 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 = 21;
    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 enjoy it.


Related articles: