Chào các bạn , lâu rồi mình chưa post bài mới nên hôm nay mình sẽ quay lại hướng dẫn các bạn code về FTP Java cơ bản, FTP là chương trình sử dụng giao thức TCP/IP để truyền gửi file giữa server và client là chính , ngoài ra còn có các thao tác khác như tạo mới file , xóa file ,……Chúng ta sẽ bắt đầu .
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
1. KẾT NỐI VỚI SERVER FTP Java
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
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
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
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 Java
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
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(); } } } } } Xem thêm các bài viết về java tại đây Liên hệ với mình Tại đây