• Java FTP按关键字批量下载文件


    一、所需jar 

    1. commons-net
    2. commons-net
    3. 3.8.0

     二、工具类

    1. import java.io.*;
    2. import java.net.URLConnection;
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import org.apache.commons.net.ftp.FTP;
    6. import org.apache.commons.net.ftp.FTPClient;
    7. import org.apache.commons.net.ftp.FTPFile;
    8. public class FTPBulkFileDownloader {
    9. public static void downloadFiles(String server, int port, String user, String pass, String remoteDir, String saveDir, String keyword) throws IOException {
    10. FTPClient ftpClient = new FTPClient();
    11. try {
    12. ftpClient.connect(server, port);
    13. ftpClient.login(user, pass);
    14. ftpClient.enterLocalPassiveMode();
    15. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    16. List filesToDownload = getFilesToDownload(ftpClient, remoteDir, keyword);
    17. for (String file : filesToDownload) {
    18. String remoteFilePath = remoteDir + "/" + file;
    19. String saveFilePath = saveDir + File.separator + file;
    20. downloadFile(ftpClient, remoteFilePath, saveFilePath);
    21. }
    22. } finally {
    23. if (ftpClient.isConnected()) {
    24. ftpClient.logout();
    25. ftpClient.disconnect();
    26. }
    27. }
    28. }
    29. private static List getFilesToDownload(FTPClient ftpClient, String remoteDir, String keyword) throws IOException {
    30. List filesToDownload = new ArrayList<>();
    31. FTPFile[] files = ftpClient.listFiles(remoteDir);
    32. for (FTPFile file : files) {
    33. if (file.isFile() && file.getName().contains(keyword)) {
    34. filesToDownload.add(file.getName());
    35. }
    36. }
    37. return filesToDownload;
    38. }
    39. private static void downloadFile(FTPClient ftpClient, String remoteFilePath, String saveFilePath) throws IOException {
    40. InputStream inputStream = ftpClient.retrieveFileStream(remoteFilePath);
    41. BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    42. FileOutputStream fileOutputStream = null;
    43. try {
    44. fileOutputStream = new FileOutputStream(saveFilePath);
    45. int bytesRead;
    46. byte[] buffer = new byte[1024];
    47. while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
    48. fileOutputStream.write(buffer, 0, bytesRead);
    49. }
    50. System.out.println("File downloaded: " + saveFilePath);
    51. } finally {
    52. if (bufferedInputStream != null) {
    53. bufferedInputStream.close();
    54. }
    55. if (fileOutputStream != null) {
    56. fileOutputStream.close();
    57. }
    58. ftpClient.completePendingCommand();
    59. }
    60. }
    61. public static void main(String[] args) {
    62. String server = "FTPIP";
    63. //FTP端口
    64. int port = 21;
    65. //用户名
    66. String user = "";
    67. //密码
    68. String pass = "";
    69. //FTP文件目录
    70. String remoteDir = "/FTP";
    71. //本地文件存放目录
    72. String saveDir = "/local";
    73. //关键字
    74. String keyword = "222";
    75. try {
    76. downloadFiles(server, port, user, pass, remoteDir, saveDir, keyword);
    77. } catch (IOException e) {
    78. e.printStackTrace();
    79. }
    80. }
    81. }

  • 相关阅读:
    J9数字论:什么是Web3.0概念?
    方法论系列:用四个金字塔来说明金字塔原理
    WebGIS开发基础
    05-jenkins与SonarQube代码审查集成
    【数据分享】2022年我国30米分辨率的坡向数据(免费获取)
    /etc/profile与~/.bash_profile的区别
    安装“react-dnd”和“react-dnd-html5-backend”无法找到“react/jsx-runtime”
    Spring Boot读取配置文件
    JAVA枚举
    Minio分布式存储系统
  • 原文地址:https://blog.csdn.net/weixin_52255395/article/details/132827029