Java FTP upload download delete files and FTP server upload folder method

  • 2020-04-01 04:20:15
  • OfStack

A JAVA implementation of FTP function code, including the server setting module, and consists of common ways to upload a file to the FTP, general methods and delete files, download files on the FTP server upload folder, folder exists and so on, to write some code in the JAVA file upload may have reference value, JAVA code FTP master file:


package ftpDemo;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class ftpUtil {
//General method for uploading files to FTP
public static void upLoadFileFtp(KmConfig kmConfig,InputStream is, String fileName){
  try {
    String ftpHost = kmConfig.getFtpHost();
    int port = kmConfig.getFtpPort();
    String userName = kmConfig.getFtpUser();
    String passWord = kmConfig.getFtpPassword();
    String path = kmConfig.getFtpPath();
    FtpClient ftpClient = new FtpClient(ftpHost, port);//FtpHost is the IP address of the FTP server,port is the landing port of the FTP server,ftpHost is String type,port is int type.
    ftpClient.login(userName, passWord);//UserName and passWord are the login userName and passWord of the FTP server, respectively
    ftpClient.binary();
    ftpClient.cd(path);//Path is the path to save the uploaded file on the FTP server.
    TelnetOutputStream telnetOut = ftpClient.put(fileName);//FileName is the name of the uploaded file
    DataOutputStream dataOut = new DataOutputStream(telnetOut);
    byte buffer[] = new byte[ * ];
    int count = ;
    while ((count = is.read(buffer)) != -) {
      dataOut.write(buffer, , count);
    }
    telnetOut.close();
    dataOut.close();
    ftpClient.closeServer();
  } catch (Exception e) {
    System.out.println(" Upload file failed! Please check the system FTP Set up the , And confirm the FTP The service start ");
  }
}
//Delete files to FTP general method
public static void deleteFileFtp(KmConfig kmConfig,String fileName){
  try {
    String ftpHost = kmConfig.getFtpHost();
    int port = kmConfig.getFtpPort();
    String userName = kmConfig.getFtpUser();
    String passWord = kmConfig.getFtpPassword();
    String path = kmConfig.getFtpPath();
    FtpClient ftpClient = new FtpClient(ftpHost, port);//FtpHost is the IP address of the FTP server,port is the landing port of the FTP server,ftpHost is String type,port is int type.
    ftpClient.login(userName, passWord);//UserName and passWord are the login userName and passWord of the FTP server, respectively
    ftpClient.binary();
    ftpClient.cd(path);//Path is the path to save the uploaded file on the FTP server.
    try {
      ftpClient.sendServer("dele " + fileName + "rn");
    } catch (Exception e) {
      System.out.println(" File deletion failed! Please check the system FTP Set up the , And confirm the FTP The service start ");
    }
    ftpClient.closeServer();
  } catch (Exception e) {
    System.out.println(" File deletion failed! ");
  }
}
//Download FTP files
public static void downloadFileFtp(KmConfig kmConfig,String fileName, String clientFileName, OutputStream outputStream){
  try {
    String ftpHost = kmConfig.getFtpHost();
    int port = kmConfig.getFtpPort();
    String userName = kmConfig.getFtpUser();
    String passWord = kmConfig.getFtpPassword();
    String path = kmConfig.getFtpPath();
    FtpClient ftpClient = new FtpClient(ftpHost, port);//FtpHost is the IP address of the FTP server,port is the landing port of the FTP server,ftpHost is String type,port is int type.
    ftpClient.login(userName, passWord);//UserName and passWord are the login userName and passWord of the FTP server, respectively
    ftpClient.binary();
    ftpClient.cd(path);//Path is the path to save the uploaded file on the FTP server.
    try {
      TelnetInputStream in = ftpClient.get(fileName);
      byte[] bytes = new byte[];
      int cnt=;
      while ((cnt=in.read(bytes,,bytes.length)) != -) {
        outputStream.write(bytes, , cnt);
      }
      outputStream.close();
      in.close();
    } catch (Exception e) {
      ftpClient.closeServer();
      e.printStackTrace();
    }
    ftpClient.closeServer();
  } catch (Exception e) {
    System.out.println(" Download file failed! Please check the system FTP Set up the , And confirm the FTP The service start ");
  }
}
//Upload files folder on FTP server
  public boolean createDir(String path,FtpClient ftpClient) throws Exception{
      //Go to the home folder
      ftpClient.cd("/home");
      //Create a remote folder
      //Remote commands include
      //USER  PORT  RETR  ALLO  DELE  SITE  XMKD  CDUP  FEAT<br>
//     PASS  PASV  STOR  REST  CWD   STAT  RMD   XCUP  OPTS<br>
//     ACCT  TYPE  APPE  RNFR  XCWD  HELP  XRMD  STOU  AUTH<br>
//     REIN  STRU  SMNT  RNTO  LIST  NOOP  PWD   SIZE  PBSZ<br>
//     QUIT  MODE  SYST  ABOR  NLST  MKD   XPWD  MDTM  PROT<br>
//Execute commands on the server. If sendServer is used to execute remote commands (local FTP commands cannot be executed), all FTP commands must be appended with /r/n<Br>
//          ftpclient.sendServer("XMKD /test/bb/r/n"); // Execute on the server FTP The command <br>
//Ftpclient. ReadServerResponse must call after sendServer <Br>
//NameList ("/test") gets a list of files in a directory <Br>
//XMKD creates a directory and creates it again when the directory exists. Br>
//XRMD delete directory <Br>
//DELE deletes files <Br>
    //Use remote commands to insert a files folder
    ftpClient.sendServer("MKD "+ path + "rn");
    //The method must be called between the two methods or the command won't work
    ftpClient.binary();
    ftpClient.readServerResponse();
    return false;
  }

  public boolean isDirExist(String dir, FtpClient ftpClient) {
     try {
      ftpClient.cd(dir);
    } catch (Exception e) {
      return false;
    }
    return true;
  }
}

The kmconfig.java code is as follows: define the parameters of the FTP server, including the login username and password, and so on.


package ftpDemo;
public class KmConfig {
  //Host IP
  private String FtpHost = "";
  //The port number
  private int FtpPort;
  //The FTP user name
  private String FtpUser = "";
  //The FTP password
  private String FtpPassword = "";
  //Directories in FTP
  private String FtpPath = "";
  public String getFtpHost() {
    return FtpHost;
  }
  public void setFtpHost(String ftpHost) {
    FtpHost = ftpHost;
  }
  public int getFtpPort() {
    return FtpPort;
  }
  public void setFtpPort(int ftpPort) {
    FtpPort = ftpPort;
  }
  public String getFtpUser() {
    return FtpUser;
  }
  public void setFtpUser(String ftpUser) {
    FtpUser = ftpUser;
  }
  public String getFtpPassword() {
    return FtpPassword;
  }
  public void setFtpPassword(String ftpPassword) {
    FtpPassword = ftpPassword;
  }
  public String getFtpPath() {
    return FtpPath;
  }
  public void setFtpPath(String ftpPath) {
    FtpPath = ftpPath;
  }
}

Here is the test code:


package ftpDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Test {
  public static void main(String[] args) {
    //Set up the FTP
    KmConfig km = new KmConfig();
    km.setFtpHost("...");
    km.setFtpPort();
    km.setFtpUser("test");
    km.setFtpPassword("");
    km.setFtpPath("KM");
    //Uploaded to the FTP
    ftpUtil util=new ftpUtil();
    File file = new File("F:/ The article .jpg");
    InputStream in;
    try {
      in = new FileInputStream(file);
      util.upLoadFileFtp(km, in, " The changed name .jpg");
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

The above code is this site to introduce you about Java FTP upload download, delete files and FTP server upload folder method, hope you like.


Related articles: