Gửi nhận file với FtpClient – Các xử lý trên FTP server

Trong post này :

  • Kết nối với server FTP
  • Lấy danh sách file trên server FTP
  • Tạo folder trên server FTP
  • Xóa folder trên server FTP
  • Gửi file lên server FTP
  • Lấy file từ server FTP
  • Xóa file trên server FTP

Bài tham khảo : Cài đặt môi trường Ftp và lệnh cơ bản của Ftp

1. Kết nối với server FTP

package com.loop.ftp;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class ConnectionFTP {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);

      System.out.println("Login is OK");
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

2. Lấy danh sách file trên server FTP

package com.loop.ftp;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class ListFileOnServerFTP {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);

      System.out.println("Login is OK");

      FTPFile[] ftpFiles = client.listFiles();
      for (FTPFile ftpFile : ftpFiles) {
        // Check if FTPFile is a regular file
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
          System.out.println("FTPFile: " + ftpFile.getName());
        }
      }
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

3. Tạo folder trên server FTP

package com.loop.ftp;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class CreateFolderIntoFTPServer {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);
      System.out.println("Login is OK");
      client.makeDirectory("/test");
      System.out.println("Tao folder test is ok");
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

4. Xóa folder trên server FTP

package com.loop.ftp;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class DeleteFolderIntoFTPServer {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);

      System.out.println("Login is OK");

      client.rmd("/test");
      System.out.println("Xoa folder test is ok");

    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

5. Gửi file lên server FTP

package com.loop.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class SendFileToServerFTP {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);

      System.out.println("Login is OK");

      File file = new File("resource/textFile.txt");
      FileInputStream fileInputStream = new FileInputStream(file);
      client.storeFile("/test/" + file.getName(), fileInputStream);

      System.out.println(" send file : " + file.getName() + " thanh cong. ");
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

6. Lấy file từ server FTP

package com.loop.ftp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class GetFileFromServerFTP {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);

      System.out.println("Login is OK");

      FTPFile ftpfile = client.listFiles()[0];
      File file = new File("resource/readme.txt");
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      client.retrieveFile(ftpfile.getName(), fileOutputStream);

      System.out.println(" get file file : " + ftpfile.getName() + " thanh cong. ");
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

7. Xóa file trên server FTP

package com.loop.ftp;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class DeleteFileOnServerFTP {

  /**
  * Create by Cuder
  */
  public static void main(String[] args) {
    FTPClient client = new FTPClient();
    try {
      client.connect("localhost", 2121);
      if (!client.login("admin", "admin")) {
        throw new Exception("Login fail!");
      }
      client.enterRemotePassiveMode();
      client.setFileType(FTP.BINARY_FILE_TYPE);

      System.out.println("Login is OK");

      String fileName = "test.txt";
      client.deleteFile(fileName);

      System.out.println("Xoa thanh cong file : " + fileName);

    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != client && client.isConnected()) {
        try {
          client.logout();
          client.disconnect();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

5 (100%) 1 vote
Đinh Thế Hiển
 

Mình là người thích viết lách, chia sẻ các trải nghiệm trong cuộc sống. Thích thể thao đặc biệt là thể hình. Hãy kết nối với mình nhé.

Click Here to Leave a Comment Below 0 comments