(企业内部) 3行代码实现ftp 上传、下载、删除操作集合

为了帮助小伙伴们便捷操作ftp,现对常用的上传、下载、删除功能进行封装,小伙伴只需要传入需要的参数操作即可!告诉小伙伴好消息,本项目我已开源,大家可以根据需要克隆run起来!!!

一、集合总览

① 上传集合

上传集合 说明
uploadLocalDirToRemotePath 上传本地文件夹至远程目录(已封装)
uploadLocalDirFiletListToRemoteDir 上传本地目录下文件列表至远程目录(已封装)
uploadLocalSpFileToRemotePath 上传本地指定文件至远程目录(已封装)
uploadLocalSpFileTypeToRemotePath 上传本地指定文件类型的文件至远程目录(已封装)
uploadLocalSpFileNameContainStrToRemotePath 上传本地指定文件名包含指定字符串的文件至远程目录(已封装)
uploadLocalDirFileNameSpPrefixFileList 上传本地指定文件名前缀的文件至远程目录(已封装)

② 下载集合

下载集合 说明
downloadRemoteDirAppointFile 下载远程目录下指定文件至本地目录(已封装)
downloadFTPDirAppointFileTypeList 下载远程目录下面的某个类型的文件列表(已封装)
downloadFTPDirFileContainInputStrList 下载远程目录下面的文件名包含指定字符串的文件列表(已封装)
downloadFTPDirFileNameprefixList 下载远程目录下面的指定文件前缀的文件列表(已封装)
downloadAllFilesInTheRemoteDirectory 下载远程目录下的文件列表(已封装)
downloadRemoteFolders 下载远程文件夹至本地目录(已封装)

③ 删除集合

删除集合 说明
delteRemoteAppointDirFileList 删除远程指定目录下的所有文件(已封装)
delteRemoteAppointFile 删除远程指定文件(已封装)
delteRemoteAppointFileTypeList 删除远程指定目录下的指定文件类型的文件列表(已封装)
delteRemoteAppointFilePrefixList 删除远程指定目录下的指定文件名前缀的文件列表(已封装)
delteRemoteFileNameContainInptStrList 删除远程指定目录下的文件名包含指定字符串的文件列表(已封装)

二、配置总览

① pom依赖

  <dependencies>
        <!--springMVC启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--文件上传-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>
        <!--实体工具类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--日志打印-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

② 全局配置文件application.yml

#端口
server:
  port: 80
#  激活环境配置
spring:
  profiles:
    active: dev

③ dev环境配置文件

###########################################################################
# IP地址 用户名 密钥 端口 本地保存路径 远程下载目录 远程指定文件 指定的文件类型 指定文件名
# 文件名中指定字符串 文件名前缀 本地文件同步目录 远程上传目录 远程删除目录
############################################################################
ftp:
  ipAddress: 192.168.55.12 #IP地址
  userName: ftpuser #用户名
  passWd: cx01 #密钥
  port: 21 #端口
  localSaveDir: #本地保存路径
  remoteDownLoadDir: #远程下载目录
  remoteDirAppointFile: #远程指定文件
  fileType: #指定的文件类型
  appointFileName: #指定文件名
  inputStr: #文件名中指定字符串
  fileNamePrefix: #文件名前缀
  localSyncFileDir: #本地文件同步目录
  romoteUpLoadePath: #远程上传目录
  romoteDelPath: #远程删除目录

④ ftp基础参数配置类BaseConfig

package com.gblfy.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author gblfy
 * @ClassNme BaseConfig
 * @Description TODO
 * @Date 2020/1/3 13:53
 * @version1.0
 */
@Data
@Component
@ConfigurationProperties(prefix = "ftp")
public class BaseConfig {
    /**
     *IP地址
     */
    private String ipAddress;
    /**
     *用户名
     */
    private String userName;
    /**
     *密钥
     */
    private String passWd;
    /**
     *端口
     */
    private int port;
    /**
     *本地保存路径
     */
    private String localSaveDir;
    /**
     *远程目录
     */
    private String remoteDownLoadDir;
    /**
     *远程指定文件
     */
    private String remoteDirAppointFile;
    /**
     *文件类型
     */
    private String fileType;
    /**
     *指定的文件名
     */
    private String appointFileName;
    /**
     *指定字符串
     */
    private String inputStr;
    /**
     *文件名前缀
     */
    private String fileNamePrefix;
    /**
     *本地同步的目录
     */
    private String localSyncFileDir;
    /**
     *远程同步的目录
     */
    private String romoteUpLoadePath;
    /**
     *删除远程的目录
     */
    private String romoteDelPath;
}


⑤ ftp公用工具类

package com.gblfy.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class FTPEnhUtils {

    private FTPClient ftpClient;
    private String strIp;
    private int intPort;
    private String user;
    private String password;
    private String mFtpPath;

    private static Logger logger = Logger.getLogger(FTPEnhUtils.class.getName());

    /**
     * 路径get方法
     *
     * @return
     * @author guobin
     */
    public String getmFtpPath() {
        return mFtpPath;
    }

    /**
     * 路径set方法
     *
     * @param mFtpPath
     * @author guobin
     */
    public void setmFtpPath(String mFtpPath) {
        this.mFtpPath = mFtpPath;
    }

    // Ftp构造函数
    public FTPEnhUtils(String strIp, int intPort, String user, String password) {
        this.strIp = strIp;
        this.intPort = intPort;
        this.user = user;
        this.password = password;
        this.ftpClient = new FTPClient();
    }

    /*********************** Common Method Start ***********************/
    /**
     * 登录到FTP服务器 成功返回true,不成功返回false
     *
     * @return
     */
    public boolean ftpLogin() {
        boolean isLogin = false;
        FTPClientConfig ftpClientConfig = new FTPClientConfig();
        ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
        this.ftpClient.setControlEncoding("UTF-8");
        this.ftpClient.configure(ftpClientConfig);
        try {
            if (this.intPort > 0) {
                this.ftpClient.connect(this.strIp, this.intPort);
            } else {
                this.ftpClient.connect(this.strIp);
            }
            // FTP服务器连接回答
            int reply = this.ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                this.ftpClient.disconnect();
                logger.info("登录FTP服务失败!");
                return isLogin;
            }
            this.ftpClient.login(this.user, this.password);
            // 设置传输协议
            this.ftpClient.enterLocalPassiveMode();
            this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            logger.info("用户 " + this.user + " 成功登陆FTP服务器");
            isLogin = true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(this.user + "登录FTP服务失败!请检查连接参数是否正确!或者网络是否通畅!" + e.getMessage());
        }
        this.ftpClient.setBufferSize(1024 * 2);
        this.ftpClient.setDataTimeout(30 * 1000);
        return isLogin;
    }

    /**
     * 退出关闭服务器链接
     */
    public void ftpLogOut() {
        if (null != this.ftpClient && this.ftpClient.isConnected()) {
            try {
                boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
                if (reuslt) {
                    logger.info("成功退出服务器");
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.info("退出FTP服务器异常!" + e.getMessage());
            } finally {
                try {
                    this.ftpClient.disconnect();// 关闭FTP服务器的连接
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.info("关闭FTP服务器的连接异常!");
                }
            }
        }
    }

    /**
     * 切换工作目录
     *
     * @param pathname
     */
    public void changeWorkingDirectory(String pathname) {
        try {
            this.ftpClient.changeWorkingDirectory(pathname);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打印出当前工作目录
     */
    public void printWorkDir() {
        try {
            logger.info(this.ftpClient.printWorkingDirectory());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*********************** Common Method Ends ***********************/
    /*********************** Upload function Parent Start ***********************/
    /**
     * 上传本地文件夹至远程目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param remoteDirectoryPath
     */
    public static void uploadLocalDirToRemotePath(String strIp, int tPort, String user, String password,
                                                  String localSyncFileDir, String remoteDirectoryPath) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        f.uploadDirectorySub(localSyncFileDir, remoteDirectoryPath);
        f.ftpLogOut();
    }

    /**
     * 上传本地目录下文件列表至远程目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localSyncFileDir
     * @param romoteUpLoadePath
     */
    public static void uploadLocalDirFiletListToRemoteDir(String strIp, int tPort, String user, String password,
                                                          String localSyncFileDir, String romoteUpLoadePath) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        f.uploadLocalDirFileListSub(localSyncFileDir, romoteUpLoadePath);
        f.ftpLogOut();
    }

    /**
     * 上传本地指定文件至远程目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localFileName
     * @param remoteDirectoryPath
     */
    public static void uploadLocalSpFileToRemotePath(String strIp, int tPort, String user, String password,
                                                     String localFileName, String remoteDirectoryPath) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        File file = new File(localFileName);
        f.uploadAppointFileToRomotePathSub(file, remoteDirectoryPath);
        f.ftpLogOut();
    }

    /**
     * 上传本地指定文件类型的文件至远程目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     */
    public static void uploadLocalSpFileTypeToRemotePath(String strIp, int tPort, String user, String password,
                                                         String localSyncFileDir, String romoteUpLoadePath, String fileType) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        f.uploadLocalDirFileTypeListToRomoteSubmethod(localSyncFileDir, romoteUpLoadePath, fileType);
        f.ftpLogOut();
    }

    /**
     * 上传本地指定文件名包含指定字符串的文件至远程目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     */
    public static void uploadLocalSpFileNameContainStrToRemotePath(String strIp, int tPort, String user,
                                                                   String password, String localSyncFileDir, String romoteUpLoadePath, String inputStr) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        f.uploadLocalSpFileNameContainStrToRemotePathSubmethod(localSyncFileDir, romoteUpLoadePath, inputStr);
        f.ftpLogOut();
    }

    /**
     * 上传本地指定文件名前缀的文件至远程目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localSyncFileDir
     * @param romoteUpLoadePath
     * @param fileNamePrefix
     */
    public static void uploadLocalDirFileNameSpPrefixFileList(String strIp, int tPort, String user, String password,
                                                              String localSyncFileDir, String romoteUpLoadePath, String fileNamePrefix) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        f.uploadLocalDirFileNameSpPrefixFileListSubmethod(localSyncFileDir, romoteUpLoadePath, fileNamePrefix);
        f.ftpLogOut();
    }

    /**
     * 上传文件夹至远程目录(子方法)
     *
     * @param localDirectory
     * @param remoteDirectoryPath
     * @return
     */
    public boolean uploadDirectorySub(String localDirectory, String remoteDirectoryPath) {
        File file = new File(localDirectory);
        try {
            logger.info("file.getName():" + file.getName());
            remoteDirectoryPath = remoteDirectoryPath + file.getName() + "/";
            this.ftpClient.makeDirectory(remoteDirectoryPath);
            // ftpClient.listDirectories();
        } catch (IOException e) {
            e.printStackTrace();
            logger.info(remoteDirectoryPath + "目录创建失败");
        }
        File[] fileList = file.listFiles();
        logger.info("文件数量:" + fileList.length);
        for (int i = 0; i < fileList.length; i++) {
            logger.info("文件名:" + fileList[i].getName());
            if (!fileList[i].isDirectory()) {
                String srcName = fileList[i].getPath().toString();
                uploadAppointFileToRomotePathSub(new File(srcName), remoteDirectoryPath);
            }
        }
        for (int i = 0; i < fileList.length; i++) {
            logger.info("文件名:" + fileList[i].getName());
            if (fileList[i].isDirectory()) {
                // 递归
                uploadDirectorySub(fileList[i].getPath().toString(), remoteDirectoryPath);
            }
        }
        return true;
    }

    /**
     * 上传本地指定文件至远程目录(子方法)
     *
     * @param localFile
     * @param romoteUpLoadePath
     * @return
     */
    public boolean uploadAppointFileToRomotePathSub(File localFile, String romoteUpLoadePath) {
        BufferedInputStream inStream = null;
        boolean success = false;
        try {
            // Create directory
            this.ftpClient.makeDirectory(romoteUpLoadePath);
            // Toggle directory
            this.ftpClient.changeWorkingDirectory(romoteUpLoadePath);// 改变工作路径
            inStream = new BufferedInputStream(new FileInputStream(localFile));
            logger.info(localFile.getName() + ">>>Start Upload.....");
            success = this.ftpClient.storeFile(localFile.getName(), inStream);
            if (success == true) {
                logger.info(localFile.getName() + ">>>>>Upload success");
                return success;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            logger.info(localFile + "No Found");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }



    public boolean upLoadMul(MultipartFile localFile, String romoteUpLoadePath, String upFileName) {
        BufferedInputStream inStream = null;
        boolean success = false;
        try {
            // Create directory
//			this.ftpClient.makeDirectory(romoteUpLoadePath);
            // Toggle directory
            this.ftpClient.changeWorkingDirectory(romoteUpLoadePath);// 改变工作路径
            inStream = new BufferedInputStream(localFile.getInputStream());
            logger.info(upFileName+ ">>>Start Upload.....");
            success = this.ftpClient.storeFile(upFileName, inStream);
            if (success == true) {
                logger.info(localFile.getOriginalFilename() + ">>>>>Upload success");
                return success;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            logger.info(localFile + "No Found");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }
    /**
     * 上传本地目录下文件列表至远程目录(子方法)
     *
     * @param localSyncFileDir
     * @param romoteUpLoadePath
     * @return
     */
    public boolean uploadLocalDirFileListSub(String localSyncFileDir, String romoteUpLoadePath) {
        File file = new File(localSyncFileDir);
        try {
            this.ftpClient.makeDirectory(romoteUpLoadePath);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info(romoteUpLoadePath + "目录创建失败");
        }
        File[] ftpFileList = file.listFiles();
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (File ff : ftpFileList) {
                logger.info("fileName:" + ff.getName());
                if (ff.isFile()) {
                    String fileName = ff.getPath().toString();
                    uploadAppointFileToRomotePathSub(new File(fileName), romoteUpLoadePath);
                } else if (ff.isDirectory()) {
                    uploadLocalDirFileListSub(ff.getPath().toString(), romoteUpLoadePath);
                }
            }
        }
        return true;
    }

    /**
     * 上传本地指定文件类型的文件至远程目录(子方法)
     *
     * @param localSyncFileDir
     * @param romoteUpLoadePath
     * @param fileType
     * @return
     */
    public boolean uploadLocalDirFileTypeListToRomoteSubmethod(String localSyncFileDir, String romoteUpLoadePath,
                                                               String fileType) {
        File file = new File(localSyncFileDir);
        try {
            this.ftpClient.makeDirectory(romoteUpLoadePath);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info(romoteUpLoadePath + "目录创建失败");
        }
        File[] ftpFileList = file.listFiles();
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (File ff : ftpFileList) {
                logger.info("fileName:" + ff.getName());
                if (ff.isFile() && ff.getName().endsWith(fileType)) {
                    String srcName = ff.getPath().toString();
                    uploadAppointFileToRomotePathSub(new File(srcName), romoteUpLoadePath);
                }
            }
        }
        return true;
    }

    /**
     * 上传本地指定文件名包含指定字符串的文件至远程目录(子方法)
     *
     * @param localSyncFileDir
     * @param romoteUpLoadePath
     * @param inputStr
     * @return
     */
    public boolean uploadLocalSpFileNameContainStrToRemotePathSubmethod(String localSyncFileDir,
                                                                        String romoteUpLoadePath, String inputStr) {
        File file = new File(localSyncFileDir);
        try {
            this.ftpClient.makeDirectory(romoteUpLoadePath);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info(romoteUpLoadePath + "目录创建失败");
        }
        File[] ftpFileList = file.listFiles();
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (File ff : ftpFileList) {
                logger.info("fileName:" + ff.getName());
                if (ff.isFile() && ff.getName().contains(inputStr)) {
                    String srcName = ff.getPath().toString();
                    uploadAppointFileToRomotePathSub(new File(srcName), romoteUpLoadePath);
                }
            }
        }
        return true;
    }

    /**
     * 上传本地指定文件名前缀的文件至远程目录(子方法)
     *
     * @param localSyncFileDir
     * @param romoteUpLoadePath
     * @param fileNamePrefix
     * @return
     */
    public boolean uploadLocalDirFileNameSpPrefixFileListSubmethod(String localSyncFileDir, String romoteUpLoadePath,
                                                                   String fileNamePrefix) {
        File file = new File(localSyncFileDir);
        try {
            this.ftpClient.makeDirectory(romoteUpLoadePath);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info(romoteUpLoadePath + "目录创建失败");
        }
        File[] ftpFileList = file.listFiles();
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (File ff : ftpFileList) {
                logger.info("fileName:" + ff.getName());
                if (ff.isFile() && ff.getName().startsWith(fileNamePrefix)) {
                    String srcName = ff.getPath().toString();
                    uploadAppointFileToRomotePathSub(new File(srcName), romoteUpLoadePath);
                }
            }
        }
        return true;
    }

    /*********************** Upload function Parent End ***********************/

    /*********************** download function Parent Start ***********************/
    /**
     * 下载远程文件夹至本地目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localDirectory
     * @param remoteDirectoryPath
     */
    public static void downloadRemoteFolders(String strIp, int tPort, String user, String password,
                                             String localDirectory, String remoteDirectoryPath) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        f.downLoadDirectorySub(localDirectory, remoteDirectoryPath);
        f.ftpLogOut();
    }

    /**
     * 下载远程目录下的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localDirectory
     * @param remoteDirectoryPath
     */
    public static void downloadAllFilesInTheRemoteDirectory(String strIp, int tPort, String user, String password,
                                                            String localDirectory, String remoteDirectoryPath) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        List<String> fileNames;
        try {
            fileNames = f.getFTPCurrentDirAllFileList(remoteDirectoryPath);
            // Number of printed files
            logger.info("fileNames.length : " + fileNames.size());
            if (fileNames != null) {
                for (String remoteFileName : fileNames) {
                    logger.info("File name under directory : " + remoteFileName);
                    f.downloadFileSub(remoteFileName, localDirectory, remoteDirectoryPath);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            f.ftpLogOut();
        }
    }

    /**
     * 下载远程目录下指定文件至本地目录(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param appointFileName
     * @param localDirectory
     * @param remoteDirectoryPath
     */
    public static void downloadRemoteDirAppointFile(String strIp, int tPort, String user, String password,
                                                    String appointFileName, String localDirectory, String remoteDirectoryPath) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        List<String> fileNames;
        try {
            fileNames = f.getFTPDirAppointFileSub(remoteDirectoryPath, appointFileName);
            // Number of printed files
            logger.info("fileNames.length : " + fileNames.size());
            if (fileNames != null) {
                for (String remoteFileName : fileNames) {
                    logger.info("File name under directory : " + remoteFileName);
                    f.downloadFileSub(remoteFileName, localDirectory, remoteDirectoryPath);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            f.ftpLogOut();
        }
    }

    /**
     * 下载远程目录下面的某个类型的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localDirectory
     * @param remoteDirectoryPath
     * @param fileType
     */
    public static void downloadFTPDirAppointFileTypeList(String strIp, int tPort, String user, String password,
                                                         String localDirectory, String remoteDirectoryPath, String fileType) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        List<String> fileNames;
        try {
            fileNames = f.getFTPDirTypeFileListSub(remoteDirectoryPath, fileType);
            // Number of printed files
            logger.info("fileNames.length : " + fileNames.size());
            if (fileNames != null) {
                for (String remoteFileName : fileNames) {
                    logger.info("File name under directory : " + remoteFileName);
                    f.downloadFileSub(remoteFileName, localDirectory, remoteDirectoryPath);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            f.ftpLogOut();
        }
    }

    /**
     * 下载远程目录下面的文件名包含指定字符串的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localDirectory
     * @param remoteDirectoryPath
     * @param inputStr
     */
    public static void downloadFTPDirFileContainInputStrList(String strIp, int tPort, String user, String password,
                                                             String localDirectory, String remoteDirectoryPath, String inputStr) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        List<String> fileNames;
        try {
            fileNames = f.getFTPDirFileNameContainInputStrFileListsub(remoteDirectoryPath, inputStr);
            // Number of printed files
            logger.info("fileNames.length : " + fileNames.size());
            if (fileNames != null) {
                for (String remoteFileName : fileNames) {
                    logger.info("File name under directory : " + remoteFileName);
                    f.downloadFileSub(remoteFileName, localDirectory, remoteDirectoryPath);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            f.ftpLogOut();
        }
    }

    /**
     * 下载远程目录下面的指定文件前缀的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param localDirectory
     * @param remoteDirectoryPath
     * @param fileNamePrefix
     */
    public static void downloadFTPDirFileNameprefixList(String strIp, int tPort, String user, String password,
                                                        String localDirectory, String remoteDirectoryPath, String fileNamePrefix) {
        FTPEnhUtils f = new FTPEnhUtils(strIp, tPort, user, password);
        f.ftpLogin();
        List<String> fileNames;
        try {
            fileNames = f.getFTPDirFileNamePrefixFileListsub(remoteDirectoryPath, fileNamePrefix);
            // Number of printed files
            logger.info("fileNames.length : " + fileNames.size());
            if (fileNames != null) {
                for (String remoteFileName : fileNames) {
                    logger.info("File name under directory : " + remoteFileName);
                    f.downloadFileSub(remoteFileName, localDirectory, remoteDirectoryPath);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            f.ftpLogOut();
        }
    }

    /**
     * 获取FTP当前目录下面指定文件(子方法)
     *
     * @param remoteDirectory
     * @return
     * @throws ParseException
     */
    public List<String> getFTPDirAppointFileSub(String remoteDirectory, String appointFileName) throws ParseException {
        List<String> fileLists = new ArrayList<String>();
        // Get all file names in the specified directory
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remoteDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (int i = 0; i < ftpFileList.length; i++) {
                FTPFile file = ftpFileList[i];
                if (file.isFile() && file.getName().equals(appointFileName)) {
                    logger.info("fileName:" + file.getName());
                    fileLists.add(file.getName());
                }
            }
        }
        return fileLists;
    }

    /***
     * 下载文件(公用方法)
     *
     * @param remoteFileName     待下载文件名称
     * @param localDires         下载到当地那个路径下
     * @param remoteDownLoadPath remoteFileName所在的路径
     */
    public boolean downloadFileSub(String remoteFileName, String localDires, String remoteDownLoadPath) {
        String strFilePath = localDires + "/" + remoteFileName;
        BufferedOutputStream outStream = null;
        boolean success = false;
        try {
            if (!(new File(localDires).exists())) {
                new File(localDires).mkdirs();
            }
            this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
            outStream = new BufferedOutputStream(new FileOutputStream(strFilePath));
            logger.info(remoteFileName + "开始下载....");
            long beforeTime = System.currentTimeMillis();
            success = this.ftpClient.retrieveFile(remoteFileName, outStream);
            long afterTime = System.currentTimeMillis();
            if (success == true) {
                double time = (afterTime - beforeTime) / 1000.0;
                File file = new File(strFilePath);
                long length = file.length();
                double downloadSpeed = (length / (time * 1024));
                downloadSpeed = (double) Math.round(downloadSpeed * 100) / 100;
                logger.info(remoteFileName + "成功下载到" + strFilePath + "用时" + time + "秒." + "平均下载速度:" + downloadSpeed
                        + "KB/秒");
                return success;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(remoteFileName + "下载失败");
        } finally {
            if (null != outStream) {
                try {
                    outStream.flush();
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (success == false) {
            logger.info(remoteFileName + "下载失败!!!");
        }
        return success;
    }
    /***
     * 下载文件(公用方法)
     *
     * @param remoteFileName     待下载文件名称
     * @param remoteDownLoadPath remoteFileName所在的路径
     */
    public InputStream downloadFile(String remoteFileName,  String remoteDownLoadPath) {
        BufferedOutputStream outStream = null;
        boolean success = false;
        try {

            this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
            logger.info(remoteFileName + "开始下载1....");
            ftpClient.enterLocalPassiveMode();
            logger.info(remoteFileName + "开始下载2....");
            ftpClient.configure(new FTPClientConfig(remoteDownLoadPath)); //这里记得改成你放的位置
//			outStream = new BufferedOutputStream(new FileOutputStream(remoteDownLoadPath+remoteFileName));
            logger.info(remoteFileName + "开始下载3....");
            InputStream in = ftpClient.retrieveFileStream(remoteFileName);
            logger.info(remoteFileName + "开始下载....");

            return in;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    /***
     * 下载文件夹(子方法)
     *
     * @param localDirectoryPath 本地地址
     * @param remoteDirectory    远程文件夹
     */
    public boolean downLoadDirectorySub(String localDirectoryPath, String remoteDirectory) {
        try {
            String fileName = new File(remoteDirectory).getName();
            localDirectoryPath = localDirectoryPath + fileName + File.separator;
            new File(localDirectoryPath).mkdirs();
            this.ftpClient.enterLocalPassiveMode();
            FTPFile[] fileList = this.ftpClient.listFiles(remoteDirectory);
            for (int i = 0; i < fileList.length; i++) {
                if (!fileList[i].isDirectory()) {
                    downloadFileSub(fileList[i].getName(), localDirectoryPath, remoteDirectory);
                }
            }
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) {
                    String strRemoteDirectoryPath = remoteDirectory + "/" + fileList[i].getName();
                    downLoadDirectorySub(localDirectoryPath, strRemoteDirectoryPath);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("下载文件夹失败");
            return false;
        }
        return true;
    }

    /**
     * 获取FTP当前目录下面所有文件(子方法)
     *
     * @param remoteDirectory
     * @return
     * @throws ParseException
     */
    public List<String> getFTPCurrentDirAllFileList(String remoteDirectory) throws ParseException {
        List<String> fileLists = new ArrayList<String>();
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remoteDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (FTPFile ff : ftpFileList) {
                if (ff.isFile()) {
                    logger.info("fileNme:" + ff.getName());
                    fileLists.add(ff.getName());
                }
            }
        }
        return fileLists;
    }


    public List<String> getFTPCurrentDirAllFileList2(String remoteDirectory,String filename) throws ParseException {
        List<String> fileLists = new ArrayList<String>();
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remoteDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (FTPFile ff : ftpFileList) {
                if (ff.isFile()&&ff.getName().equals(filename)) {
                    logger.info("fileNme:" + ff.getName());
                    fileLists.add(ff.getName());
                }
            }
        }
        return fileLists;
    }

    /**
     * 获取远程目录下面的某个类型的文件(子方法)
     *
     * @param remoteDirectory
     * @return
     * @throws ParseException
     */
    public List<String> getFTPDirTypeFileListSub(String remoteDirectory, String fileType) throws ParseException {
        List<String> fileLists = new ArrayList<String>();
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remoteDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (FTPFile ff : ftpFileList) {
                logger.info("fileNme:" + ff.getName());
                if (ff.isFile() && ff.getName().endsWith(fileType)) {
                    fileLists.add(ff.getName());
                }
            }
        }
        return fileLists;
    }

    /**
     * 获取远程目录下面文件名包含地址字符串的文件(子方法)
     *
     * @param remoteDirectory
     * @return
     * @throws ParseException
     */
    public List<String> getFTPDirFileNameContainInputStrFileListsub(String remoteDirectory, String inputStr)
            throws ParseException {
        List<String> fileLists = new ArrayList<String>();
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remoteDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (FTPFile ff : ftpFileList) {
                logger.info("fileNme:" + ff.getName());
                if (ff.isFile() && ff.getName().contains(inputStr)) {
                    fileLists.add(ff.getName());
                }
            }
        }
        return fileLists;
    }

    /**
     * 下载远程目录下面的指定文件前缀的文件列表(子方法)
     *
     * @param remoteDirectory
     * @param fileNamePrefix
     * @return
     * @throws ParseException
     */
    public List<String> getFTPDirFileNamePrefixFileListsub(String remoteDirectory, String fileNamePrefix)
            throws ParseException {
        List<String> fileLists = new ArrayList<String>();
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remoteDirectory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (FTPFile ff : ftpFileList) {
                logger.info("fileNme:" + ff.getName());
                if (ff.isFile() && ff.getName().startsWith(fileNamePrefix)) {
                    fileLists.add(ff.getName());
                }
            }
        }
        return fileLists;
    }

    /*********************** download function Parent End ***********************/
    /***********************
     * deleteRemote function Parent Start
     ***********************/
    /**
     * 删除远程指定目录下的所有文件(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param remotePath
     */
    public static void delteRemoteAppointDirFileList(String strIp, int tPort, String user, String password,
                                                     String remotePath) {
        FTPEnhUtils ftp = new FTPEnhUtils(strIp, tPort, user, password);
        ftp.ftpLogin();
        ftp.delRemoteDirFileList(remotePath);
        ftp.ftpLogOut();
    }

    /**
     * 删除远程指定文件(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param remotePath
     * @param remoteAppointFileName
     */
    public static void delteRemoteAppointFile(String strIp, int tPort, String user, String password, String remotePath,
                                              String remoteAppointFileName) {
        FTPEnhUtils ftp = new FTPEnhUtils(strIp, tPort, user, password);
        ftp.ftpLogin();
        String remoteDelFilePath = remotePath + remoteAppointFileName;
        ftp.delete(remoteDelFilePath);
        ftp.ftpLogOut();
    }

    /**
     * 删除远程指定目录下的指定文件类型的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param remotePath
     * @param fileType
     */
    public static void delteRemoteAppointFileTypeList(String strIp, int tPort, String user, String password,
                                                      String remotePath, String fileType) {
        FTPEnhUtils ftp = new FTPEnhUtils(strIp, tPort, user, password);
        ftp.ftpLogin();
        ftp.delteRemoteAppointFileTypeListSub(remotePath, fileType);
        ftp.ftpLogOut();
    }

    /**
     * 删除远程指定目录下的指定文件名前缀的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param remotePath
     * @param fileNamePrefix
     */
    public static void delteRemoteAppointFilePrefixList(String strIp, int tPort, String user, String password,
                                                        String remotePath, String fileNamePrefix) {
        FTPEnhUtils ftp = new FTPEnhUtils(strIp, tPort, user, password);
        ftp.ftpLogin();
        ftp.delteRemoteAppointFilePrefixListSub(remotePath, fileNamePrefix);
        ftp.ftpLogOut();
    }

    /**
     * 删除远程指定目录下的文件名包含指定字符串的文件列表(已封装)
     *
     * @param strIp
     * @param tPort
     * @param user
     * @param password
     * @param remotePath
     * @param inputStr
     */
    public static void delteRemoteFileNameContainInptStrList(String strIp, int tPort, String user, String password,
                                                             String remotePath, String inputStr) {
        FTPEnhUtils ftp = new FTPEnhUtils(strIp, tPort, user, password);
        ftp.ftpLogin();
        ftp.delteRemoteFileNameContainInptStrListSub(remotePath, inputStr);
        ftp.ftpLogOut();
    }

    /**
     * 删除远程指定目录下的所有文件(子方法)
     *
     * @param remotePath
     */
    public void delRemoteDirFileList(String remotePath) {
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remotePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (int i = 0; i < ftpFileList.length; i++) {
                FTPFile file = ftpFileList[i];
                if (file.isFile()) {
                    logger.info("fileName:" + file.getName());
                    try {
                        this.ftpClient.deleteFile(remotePath + file.getName());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 删除远程指定目录下的指定文件类型的文件列表(子方法)
     *
     * @param remotePath
     * @param fileType
     */
    public void delteRemoteAppointFileTypeListSub(String remotePath, String fileType) {
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remotePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (int i = 0; i < ftpFileList.length; i++) {
                FTPFile file = ftpFileList[i];
                if (file.isFile() && file.getName().endsWith(fileType)) {
                    logger.info("fileName:" + file.getName());
                    try {
                        this.ftpClient.deleteFile(remotePath + file.getName());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 删除远程指定目录下的指定文件名前缀的文件列表(已封装)
     *
     * @param remotePath
     * @param fileNamePrefix
     */
    public void delteRemoteAppointFilePrefixListSub(String remotePath, String fileNamePrefix) {
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remotePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (int i = 0; i < ftpFileList.length; i++) {
                FTPFile file = ftpFileList[i];
                if (file.isFile() && file.getName().startsWith(fileNamePrefix)) {
                    logger.info("fileName:" + file.getName());
                    try {
                        this.ftpClient.deleteFile(remotePath + file.getName());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 删除远程指定目录下的指定文件名前缀的文件列表(子方法)
     *
     * @param remotePath
     * @param inputStr
     */
    public void delteRemoteFileNameContainInptStrListSub(String remotePath, String inputStr) {
        FTPFile[] ftpFileList = null;
        try {
            ftpFileList = this.ftpClient.listFiles(remotePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (ftpFileList != null && ftpFileList.length > 0) {
            for (int i = 0; i < ftpFileList.length; i++) {
                FTPFile file = ftpFileList[i];
                if (file.isFile() && file.getName().contains(inputStr)) {
                    logger.info("fileName:" + file.getName());
                    try {
                        this.ftpClient.deleteFile(remotePath + file.getName());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 删除远程指定文件(子方法)
     *
     * @param pathname
     */
    public void delete(String pathname) {

        try {
            this.ftpClient.deleteFile(pathname);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***********************
     * deleteRemote function Parent End
     ***********************/
    /***********************
     * deleteLocal function Parent Start
     ***********************/
    /**
     * 删除本地目录下文件列表
     *
     * @param mDeleteLocalFileListPath
     */
    public static void delLocalDirFileList(String mDeleteLocalFileListPath) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile()) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录指定文件类型的文件列表(忽略大小写)
     *
     * @param mDeleteLocalFileListPath
     * @param mFileType
     */
    public static void delLocalDirFileList(String mDeleteLocalFileListPath, String mFileType) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().toLowerCase().endsWith(mFileType)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录指定文件类型的文件并且文件名中包含传入日期的文件列表(忽略大小写)
     *
     * @param mDeleteLocalFileListPath
     * @param mFileType
     * @param mInputDate
     */
    public static void delLocalDirFileList(String mDeleteLocalFileListPath, String mFileType, String mInputDate) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().toLowerCase().endsWith(mFileType) && ff.getName().contains(mInputDate)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录下面文件名中包含传入日期的文件列表
     *
     */
    public static void delLocalDirFileNameContainInputDate(String mDeleteLocalFileListPath, String mInputDate) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().contains(mInputDate)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录下面文件名中包含传入字符串的文件列表(忽略大小写)
     *
     */
    public static void delLocalDirFileNameContainInputStr(String mDeleteLocalFileListPath, String mInputStr) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().toLowerCase().contains(mInputStr)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录下面文件名指定前缀并且包含传入日期的文件列表(忽略大小写)
     *
     * @param mDeleteLocalFileListPath
     * @param mPrefix
     * @param mInputDate
     */
    public static void delLocalDirFileNameContainInputDate(String mDeleteLocalFileListPath, String mPrefix,
                                                           String mInputDate) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().toLowerCase().startsWith(mPrefix) && ff.getName().contains(mInputDate)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录下面文件名指定前缀的文件列表(忽略大小写)
     *
     * @param mDeleteLocalFileListPath
     * @param mPrefix
     */
    public static void delLocalDirFileNameSpPrefixFileList(String mDeleteLocalFileListPath, String mPrefix) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().toLowerCase().startsWith(mPrefix)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录下面文件名指定前缀和索引的文件列表(忽略大小写)
     *
     * @param mDeleteLocalFileListPath
     * @param mPrefix
     */
    public static void delLocalDirFileNameSpPrefixFileList(String mDeleteLocalFileListPath, String mPrefix, int index) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().toLowerCase().startsWith(mPrefix, index)) {
                ff.delete();
            }
        }
    }

    /**
     * 删除本地目录指定文件类型的文件列表(不忽略大小写)
     *
     * @param mDeleteLocalFileListPath
     * @param mFileType
     */
    public static void delLocalDirFileList2(String mDeleteLocalFileListPath, String mFileType) {
        File file = new File(mDeleteLocalFileListPath);
        File[] f1 = file.listFiles();
        for (File ff : f1) {
            System.out.println("fileName:" + ff.getName());
            if (ff.isFile() && ff.getName().endsWith(mFileType)) {
                ff.delete();
            }
        }
    }

    /***********************
     * deleteLocal function Parent End
     ***********************/
    /**
     * 获取FTP目录当前日期下面所有文件
     *
     * @return
     * @throws ParseException
     */
    public  List<String> getCurrentDirAllFileList(String localPath, String strDate) throws ParseException {
        List<String> list = new ArrayList<String>();
        // 获得指定目录下所有文件名
        File file = new File(localPath);
        File[] listFiles = null;
        listFiles = file.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File ff : listFiles) {
                if (ff.isFile() && ff.getName().contains(strDate)) {
                    list.add(ff.getName());
                }
            }
        }
        return list;
    }

    // FtpClient的Set 和 Get 函数
    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
}

三、ftp实战

① 下载集合测试

package com.gblfy.controller;

import com.gblfy.config.BaseConfig;
import com.gblfy.utils.FTPEnhUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author gblfy
 * @Description 下载集合
 * @Date 2020/1/2 15:05
 * @version1.0
 */
@Controller
public class DownloadAssemble {

    @Autowired
    private BaseConfig baseConfig;

    /******************************* DownloadAssemble Start *******************************/

    /**
     * 下载远程目录下指定文件至本地目录(已封装)
     *
     * @return
     */
    @GetMapping("/downloadRemoteDirAppointFile")
    @ResponseBody
    public String downloadRemoteDirAppointFile() {
        FTPEnhUtils.downloadRemoteDirAppointFile(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getRemoteDirAppointFile(), baseConfig.getLocalSaveDir(), baseConfig.getRemoteDownLoadDir());
        return "下载完成!!!";
    }

    /**
     * 下载远程目录下面的某个类型的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/downloadFTPDirAppointFileTypeList")
    @ResponseBody
    public String downloadFTPDirAppointFileTypeList() {
        FTPEnhUtils.downloadFTPDirAppointFileTypeList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSaveDir(), baseConfig.getRemoteDownLoadDir(), baseConfig.getFileType());
        return "下载完成!!!";
    }

    /**
     * 下载远程目录下面的文件名包含指定字符串的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/downloadFTPDirFileContainInputStrList")
    @ResponseBody
    public String downloadFTPDirFileContainInputStrList() {
        FTPEnhUtils.downloadFTPDirFileContainInputStrList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSaveDir(), baseConfig.getRemoteDownLoadDir(), baseConfig.getInputStr());
        return "下载完成!!!";
    }

    /**
     * 下载远程目录下面的指定文件前缀的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/downloadFTPDirFileNameprefixList")
    @ResponseBody
    public String downloadFTPDirFileNameprefixList() {
        FTPEnhUtils.downloadFTPDirFileNameprefixList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSaveDir(), baseConfig.getRemoteDownLoadDir(), baseConfig.getFileNamePrefix());
        return "下载完成!!!";
    }

    /**
     * 下载远程目录下的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/downloadAllFilesInTheRemoteDirectory")
    @ResponseBody
    public String downloadAllFilesInTheRemoteDirectory() {
        FTPEnhUtils.downloadAllFilesInTheRemoteDirectory(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSaveDir(), baseConfig.getRemoteDownLoadDir());
        return "下载完成!!!";
    }

    /**
     * 下载远程文件夹至本地目录(已封装)
     *
     * @return
     */
    @GetMapping("/downloadRemoteFolders")
    @ResponseBody
    public String downloadRemoteFolders() {
        FTPEnhUtils.downloadRemoteFolders(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSaveDir(), baseConfig.getRemoteDownLoadDir());
        return "下载完成!!!";
    }

/******************************* DownloadAssemble End *******************************/
}

② 上传集合测试

package com.gblfy.controller;

import com.gblfy.config.BaseConfig;
import com.gblfy.utils.FTPEnhUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author gblfy
 * @ClassNme UploadAssemble
 * @Description 上传集合
 * @Date 2020/1/3 14:52
 * @version1.0
 */
@Controller
public class UploadAssemble {

    @Autowired
    private BaseConfig baseConfig;

    /******************************* UploadAssemble Start *******************************/

    /**
     * 上传本地文件夹至远程目录(已封装)
     *
     * @return
     */
    @GetMapping("/uploadLocalDirToRemotePath")
    @ResponseBody
    public String uploadLocalDirToRemotePath() {
        FTPEnhUtils.uploadLocalDirToRemotePath(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSyncFileDir(), baseConfig.getRomoteUpLoadePath());
        return "上传完成!!!";
    }

    /**
     * 上传本地目录下文件列表至远程目录(已封装)
     *
     * @return
     */
    @GetMapping("/uploadLocalDirFiletListToRemoteDir")
    @ResponseBody
    public String uploadLocalDirFiletListToRemoteDir() {
        FTPEnhUtils.uploadLocalDirFiletListToRemoteDir(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSyncFileDir(), baseConfig.getRomoteUpLoadePath());
        return "上传完成!!!";
    }

    /**
     * 上传本地指定文件至远程目录(已封装)
     *
     * @return
     */
    @GetMapping("/uploadLocalSpFileToRemotePath")
    @ResponseBody
    public String uploadLocalSpFileToRemotePath() {
        FTPEnhUtils.uploadLocalSpFileToRemotePath(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getAppointFileName(), baseConfig.getRomoteUpLoadePath());
        return "上传完成!!!";
    }

    /**
     * 上传本地指定文件类型的文件至远程目录(已封装)
     *
     * @return
     */
    @GetMapping("/uploadLocalSpFileTypeToRemotePath")
    @ResponseBody
    public String uploadLocalSpFileTypeToRemotePath() {
        FTPEnhUtils.uploadLocalSpFileTypeToRemotePath(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSyncFileDir(), baseConfig.getRomoteUpLoadePath(), baseConfig.getFileType());
        return "上传完成!!!";
    }

    /**
     * 上传本地指定文件名包含指定字符串的文件至远程目录(已封装)
     *
     * @return
     */
    @GetMapping("/uploadLocalSpFileNameContainStrToRemotePath")
    @ResponseBody
    public String uploadLocalSpFileNameContainStrToRemotePath() {
        FTPEnhUtils.uploadLocalSpFileNameContainStrToRemotePath(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSyncFileDir(), baseConfig.getRomoteUpLoadePath(), baseConfig.getInputStr());
        return "上传完成!!!";
    }

    /**
     * 上传本地指定文件名前缀的文件至远程目录(已封装)
     *
     * @return
     */
    @GetMapping("/uploadLocalDirFileNameSpPrefixFileList")
    @ResponseBody
    public String uploadLocalDirFileNameSpPrefixFileList() {
        FTPEnhUtils.uploadLocalDirFileNameSpPrefixFileList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getLocalSyncFileDir(), baseConfig.getRomoteUpLoadePath(), baseConfig.getFileNamePrefix());
        return "上传完成!!!";
    }
    /******************************* UploadAssemble End *******************************/

}

③ 删除集合测试

package com.gblfy.controller;

import com.gblfy.config.BaseConfig;
import com.gblfy.utils.FTPEnhUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author gblfy
 * @Description 删除集合
 * @Date 2020/1/2 15:05
 * @version1.0
 */
@Controller
public class DelAssemble {

    @Autowired
    private BaseConfig baseConfig;

    /******************************* DelAssemble Start *******************************/

    /**
     * 删除远程指定目录下的所有文件(已封装)
     *
     * @return
     */
    @GetMapping("/delteRemoteAppointDirFileList")
    @ResponseBody
    public String delteRemoteAppointDirFileList() {
        FTPEnhUtils.delteRemoteAppointDirFileList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getRomoteDelPath());
        return "删除完成!!!";
    }

    /**
     * 删除远程指定文件(已封装)
     *
     * @return
     */
    @GetMapping("/delteRemoteAppointFile")
    @ResponseBody
    public String delteRemoteAppointFile() {
        FTPEnhUtils.delteRemoteAppointFile(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getRomoteDelPath(), baseConfig.getAppointFileName());
        return "删除完成!!!";
    }

    /**
     * 删除远程指定目录下的指定文件类型的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/delteRemoteAppointFileTypeList")
    @ResponseBody
    public String delteRemoteAppointFileTypeList() {
        FTPEnhUtils.delteRemoteAppointFileTypeList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getRomoteDelPath(), baseConfig.getFileType());
        return "删除完成!!!";
    }

    /**
     * 删除远程指定目录下的指定文件名前缀的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/delteRemoteAppointFilePrefixList")
    @ResponseBody
    public String delteRemoteAppointFilePrefixList() {
        FTPEnhUtils.delteRemoteAppointFilePrefixList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getRomoteDelPath(), baseConfig.getFileNamePrefix());
        return "删除完成!!!";
    }

    /**
     * 删除远程指定目录下的文件名包含指定字符串的文件列表(已封装)
     *
     * @return
     */
    @GetMapping("/delteRemoteFileNameContainInptStrList")
    @ResponseBody
    public String delteRemoteFileNameContainInptStrList() {
        FTPEnhUtils.delteRemoteFileNameContainInptStrList(baseConfig.getIpAddress(), baseConfig.getPort(), baseConfig.getUserName(),
                baseConfig.getPassWd(), baseConfig.getRomoteDelPath(), baseConfig.getInputStr());
        return "删除完成!!!";
    }

/******************************* DelAssemble End *******************************/
}

项目源码:

https://github.com/gb-heima/file-assemble

发布了745 篇原创文章 · 获赞 92 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/weixin_40816738/article/details/103822638