Method for uploading java files to ftp server

  • 2020-12-21 18:03:23
  • OfStack

ftp file upload is realized with java. I used ES3en-ES4en-1.4.1.ES5en. It includes a number of java network programming toolkits.

1. Load the ES9en-ES10en-1.4.1.ES11en package into the project project.

2. Look at the following code:


import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPReply; 
 
public class FileTool { 
 
  /** 
   * Description:  to FTP Server upload file  
   * @Version   1.0 
   * @param url FTP The server hostname 
   * @param port FTP Server port  
   * @param username FTP Login account  
   * @param password FTP The login password  
   * @param path FTP Server save directory  
   * @param filename  Uploaded to the FTP File name on the server  
   * @param input   The input stream  
   * @return  Successfully returns true Otherwise return false * 
   */ 
  public static boolean uploadFile(String url,// FTP The server hostname 
      int port,// FTP Server port  
      String username, // FTP Login account  
      String password, // FTP The login password  
      String path, // FTP Server save directory  
      String filename, //  Uploaded to the FTP File name on the server  
      InputStream input //  The input stream  
  ){ 
    boolean success = false; 
    FTPClient ftp = new FTPClient(); 
    ftp.setControlEncoding("GBK"); 
    try { 
      int reply; 
      ftp.connect(url, port);//  The connection FTP The server  
      //  If you use the default port, you can use it ftp.connect(url) The way to connect directly FTP The server  
      ftp.login(username, password);//  The login  
      reply = ftp.getReplyCode(); 
      if (!FTPReply.isPositiveCompletion(reply)) { 
        ftp.disconnect(); 
        return success; 
      } 
      ftp.setFileType(FTPClient.BINARY_FILE_TYPE); 
      ftp.makeDirectory(path); 
      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; 
  } 
 
  /** 
   *  Upload local files to FTP On the server  * 
   */ 
  public static void upLoadFromProduction(String url,// FTP The server hostname 
      int port,// FTP Server port  
      String username, // FTP Login account  
      String password, // FTP The login password  
      String path, // FTP Server save directory  
      String filename, //  Uploaded to the FTP File name on the server  
      String orginfilename //  Input stream file name  
    ) { 
    try { 
      FileInputStream in = new FileInputStream(new File(orginfilename)); 
      boolean flag = uploadFile(url, port, username, password, path,filename, in); 
      System.out.println(flag); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
   // test  
  public static void main(String[] args) { 
     
    upLoadFromProduction("192.168.13.32", 21, "hanshibo", "han", " The Hanshwave test ", "hanshibo.doc", "E:/temp/H2 Database usage .doc"); 
  } 
} 

3. Run directly. You can upload the specified file to the ftp server. The jar package can be downloaded from my resources.


Related articles: