Spring boot FTP 连接池上传

本文介绍了如何在Spring Boot应用中使用FTP连接池进行文件批量上传,包括引入FTP相关库、配置FTP参数、创建FTPClient工厂、定义FTP连接池、实现上传下载功能以及进行测试。详细步骤涵盖从配置到实际操作的全过程。

Spring boot FTP 连接池上传,批量上传,ftpClient 上传,支持多连接快速切换上传

 

1.引入jar

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.8.0</version>
</dependency>

2. FTP 配置

ftp:
  ftpServer0:
    hostname: localhost
    port: 21
    username: guest
    password: b_27Fir#MwA@Rei_nfO
    encoding: UTF-8
  ftpServer1:
     hostname: localhost
     port: 21
     username: guest
     password: b_27Fir#MwA@Rei_nfO
     encoding: UTF-8

3. 读取配置

@Component
@ConfigurationProperties(prefix = "ftp")
public class FTPProperties {

    private FtpServer ftpServer0 = new FtpServer();
    private FtpServer ftpServer1 = new FtpServer();

    @Data
    public class FtpServer {
        private String hostname;

        private Integer port;

        private String username;

        private String password;
        /**
         * 编码
         */
        private String encoding;
    }
}

 4. 添加springboot 配置,注册bean

@Configuration
@EnableConfigurationProperties(FTPProperties.class)
public class FTPConfig {
}

5. 创建 FTPClient 工厂

@Slf4j
public class FTPClientFactory extends BasePooledObjectFactory<FTPClient> {

    public FTPProperties.FtpServer ftpServer;

    public FTPClientFactory(FTPProperties.FtpServer ftpServer) {
            this.ftpServer = ftpServer;
    }

    @Override
    public FTPClient create() throws Exception {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(ftpServer.getHostname(), ftpServer.getPort());
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            ftpClient.disconnect();
            log.warn("FTP服务器拒绝连接");
            return null;
        }
        log.info("连接FTP服务器成功返回码 ==> {}", ftpClient.getReplyCode());
        ftpClient.login(ftpServer.getUsername(), ftpServer.getPassword());
        log.info("登录FTP服务器成功返回码 ==> {}", ftpClient.getReplyCode());
        ftpClient.enterLocalPassiveMode();
        return ftpClient;
    }

    /**
     * 用PooledObject封装对象放入池中
     * @param ftpClient
     * @return
     */
    @Override
    public PooledObject<FTPClient> wrap(FTPClient ftpClient) {
        return new DefaultPooledObject<>(ftpClient);
    }

    /**
     * 销毁
     * @param ftpPooled
     * @throws Exception
     */
    @Override
    public void destroyObject(PooledObject<FTPClient> ftpPooled) throws Exception {
        if (null == ftpPooled) {
            return;
        }

        FTPClient ftpClient = ftpPooled.getObject();
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
            }
        } catch (IOException e) {
            log.error("登出FTP服务器失败");
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                log.error("登出FTP服务器失败");
            }
        }
    }

    /**
     * 验证FTPClient对象
     * @param ftpPooled
     * @return
     */
    @Override
    public boolean validateObject(PooledObject<FTPClient> ftpPooled) {
        try {
            FTPClient ftpClient = ftpPooled.getObject();
            return ftpClient.sendNoOp();
        } catch (IOException e) {
            log.error("验证FTP服务器失败");
        }
        return false;
    }
}

6. FTPPool 自定义连接池

/**
 * FTPClient 连接池
 */
@Slf4j
public class FTPClientPool extends BaseObjectPool<FTPClient> {

    private static final int DEFAULT_POOL_SIZE = 8;
    private final FTPClientFactory ftpClientFactory;
    private final BlockingQueue<FTPClient> ftpBlockingQueue;

    /**
     * 初始化连接池,需要注入一个工厂来提供FTPClient实例
     * @param ftpClientFactory
     * @throws Exception
     */
    public FTPClientPool(FTPClientFactory ftpClientFactory) throws Exception {
        this(DEFAULT_POOL_SIZE, ftpClientFactory);
    }

    public FTPClientPool(int poolSize, FTPClientFactory ftpClientFactory) throws Exception {
        this.ftpClientFactory = ftpClientFactory;
        ftpBlockingQueue = new ArrayBlockingQueue<>(poolSize);
        initPool(poolSize);
    }

    /**
     * 初始化连接池,需要注入一个工厂来提供FTPClient实例
     * @param maxPoolSize
     * @throws Exception
     */
    private void initPool(int maxPoolSize) throws Exception {
        for (int i = 0; i < maxPoolSize; i++) {
            // 往池中添加对象
            addObject();
        }
    }

    /**
     * 从连接池中获取对象
     * @return
     * @throws Exception
     */
    @Override
    public FTPClient borrowObject() throws Exception {
        FTPClient client = ftpBlockingQueue.take();
        if (ObjectUtils.isEmpty(client)) {
            client = ftpClientFactory.create();
            // 放入连接池
            returnObject(client);
            // 验证对象是否有效
        } else if (!ftpClientFactory.validateObject(ftpClientFactory.wrap(client))) {
            // 对无效的对象进行处理
            invalidateObject(client);
            // 创建新的对象
            client = ftpClientFactory.create();
            // 将新的对象放入连接池
            returnObject(client);
        }
        return client;
    }

    /**
     * 移除无效的对象
     * @param ftpClient
     * @throws Exception
     */
    @Override
    public void returnObject(FTPClient ftpClient) throws Exception {
        try {
            if (ftpClient != null && !ftpBlockingQueue.offer(ftpClient, 3, TimeUnit.SECONDS)) {
                ftpClientFactory.destroyObject(ftpClientFactory.wrap(ftpClient));
            }
        } catch (InterruptedException e) {
            log.error("return ftp client interrupted ...{}", e);
        }
    }

    /**
     * 移除无效的对象
     * @param ftpClient
     * @throws Exception
     */
    @Override
    public void invalidateObject(FTPClient ftpClient) throws Exception {
        try {
            ftpClient.changeWorkingDirectory("/");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ftpBlockingQueue.remove(ftpClient);
        }
    }

    /**
     * 增加一个新的链接,超时失效
     * @throws Exception
     */
    @Override
    public void addObject() throws Exception {
        // 插入对象到队列
        ftpBlockingQueue.offer(ftpClientFactory.create(), 3, TimeUnit.SECONDS);
    }

    /**
     * 关闭连接池
     */
    @Override
    public void close() {
        try {
            while (ftpBlockingQueue.iterator().hasNext()) {
                FTPClient client = ftpBlockingQueue.take();
                ftpClientFactory.destroyObject(ftpClientFactory.wrap(client));
            }
        } catch (Exception e) {
            log.error("close ftp client ftpBlockingQueue failed...{}", e);
        }
    }
}

 7. 上传,下载、

/**
 * 实现文件上传下载
 */
@Slf4j
public class FTPClientTemplate {

    private GenericObjectPool<FTPClient> ftpClientPool;


    public FTPClientTemplate(FTPClientFactory ftpClientFactory) {
        this.ftpClientPool = new GenericObjectPool<>(ftpClientFactory);
    }

    /**
     * 上传Ftp文件
     *
     * @param localFile 当地文件
     * @param remotePath 上传服务器路径 - 应该以/结束
     * @return true or false
     */
    public boolean uploadFile(File localFile, String remotePath) {
        FTPClient ftpClient = null;
        BufferedInputStream inStream = null;
        try {
            //从池中获取对象
            ftpClient = ftpClientPool.borrowObject();
            // 验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.warn("ftpServer refused connection, replyCode:{}", replyCode);
                return false;
            }
            // 改变工作路径
            ftpClient.changeWorkingDirectory(remotePath);
            inStream = new BufferedInputStream(new FileInputStream(localFile));
            log.info("start upload... {}", localFile.getName());

            final int retryTimes = 3;

            for (int j = 0; j <= retryTimes; j++) {
                boolean success = ftpClient.storeFile(localFile.getName(), inStream);
                if (success) {
                    log.info("upload file success! {}", localFile.getName());
                    return true;
                }
                log.warn("upload file failure! try uploading again... {} times", j);
            }

        } catch (FileNotFoundException e) {
            log.error("file not found!{}", localFile);
        } catch (Exception e) {
            log.error("upload file failure!", e);
        } finally {
            IOUtils.closeQuietly(inStream);
            //将对象放回池中
            ftpClientPool.returnObject(ftpClient);
        }
        return false;
    }

    /**
     * 下载文件
     *
     * @param remotePath FTP服务器文件目录
     * @param fileName   需要下载的文件名称
     * @param localPath  下载后的文件路径
     * @return true or false
     */
    public boolean downloadFile(String remotePath, String fileName, String localPath) {
        FTPClient ftpClient = null;
        OutputStream outputStream = null;
        try {
            ftpClient = ftpClientPool.borrowObject();
            // 验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.warn("ftpServer refused connection, replyCode:{}", replyCode);
                return false;
            }

            // 切换FTP目录
            ftpClient.changeWorkingDirectory(remotePath);
            FTPFile[] ftpFiles = ftpClient.listFiles();
            for (FTPFile file : ftpFiles) {
                if (fileName.equalsIgnoreCase(file.getName())) {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(localPath).append(File.separator).append(file.getName());
                    File localFile = new File(stringBuilder.toString());
                    outputStream = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(file.getName(), outputStream);
                }
            }
            ftpClient.logout();
            return true;
        } catch (Exception e) {
            log.error("download file failure!", e);
        } finally {
            IOUtils.closeQuietly(outputStream);
            ftpClientPool.returnObject(ftpClient);
        }
        return false;
    }

    /**
     * 删除文件
     *
     * @param remotePath FTP服务器保存目录
     * @param fileName   要删除的文件名称
     * @return true or false
     */
    public boolean deleteFile(String remotePath, String fileName) {
        FTPClient ftpClient = null;
        try {
            ftpClient = ftpClientPool.borrowObject();
            // 验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.warn("ftpServer refused connection, replyCode:{}", replyCode);
                return false;
            }
            // 切换FTP目录
            ftpClient.changeWorkingDirectory(remotePath);
            int delCode = ftpClient.dele(fileName);
            log.debug("delete file reply code:{}", delCode);
            return true;
        } catch (Exception e) {
            log.error("delete file failure!", e);
        } finally {
            ftpClientPool.returnObject(ftpClient);
        }
        return false;
    }


}

8. FTP 连接枚举

/**
 * FTP服务端连接
 */
@Slf4j
public class FTPServerEnumUse {

    public static FTPProperties ftpProperties= null;

    static {
        ftpProperties= SpringContextUtils.getBean("ftpProperties");
    }

    public enum FTPServerEnum{
        FTP_SERVER_NO_0(CommonConstant.FTP_SERVER_NO_0, ftpProperties.getFtpServer0())
        ,FTP_SERVER_NO_1(CommonConstant.FTP_SERVER_NO_1, ftpProperties.getFtpServer1());

        private Integer ftpServerNo;

        private FtpServer ftpServer;

        public Integer getFtpServerNo() {
            return ftpServerNo;
        }

        public FTPProperties.FtpServer getFtpServer() {
            return ftpServer;
        }

        FTPServerEnum(Integer ftpServerNo, FtpServer ftpServer) {
            this.ftpServerNo = ftpServerNo;
            this.ftpServer = ftpServer;
        }

        public static FTPServerEnum valueOf(int ftpServerNo) {
            return Arrays.asList(FTPServerEnum.values()).stream().filter(servers -> servers.getFtpServerNo() == ftpServerNo).findFirst().orElse(FTP_SERVER_NO_0);
        }
    }

    /**
     * FTP 服务器编号
     */
    public interface CommonConstant {
        Integer FTP_SERVER_NO_0 = 0;
        Integer FTP_SERVER_NO_1 = 1;
    }

}

 

9. FTP 工具

/**
 * Ftp 工具类
 */
@Slf4j
public class FTPUtil {

    private static final Integer IPHONE = 101;
    private static final Integer IPAD = 102;

    public static void uploadFile(Integer ftpServerNo, String remotePath, File file) {
        FTPServerEnumUse.FTPServerEnum ftpServerEnum = FTPServerEnumUse.FTPServerEnum.valueOf(ftpServerNo);
        FTPProperties.FtpServer _ftpServer = ftpServerEnum.getFtpServer();
        FTPClientFactory ftpClientFactory = new FTPClientFactory(_ftpServer);
        FTPClientTemplate ftpClientTemplate = new FTPClientTemplate(ftpClientFactory);
        ftpClientTemplate.uploadFile(file, remotePath);
    }


}

10. 测试

public class FtpClientTest {
    /**
     * 测试上传
     */
    @Test
    public void uploadFile() throws IOException {
        File file =  new File("C:\\Users\\Administer\\Pictures\\20191220092133.jpg");
        String remotePath= "/tmp/test/image/";
        Integer ftpNum = 0; // 切换ftp连接
        FtpUtil.uploadFile(ftpNum, remotePath, file);
    }

}

如果对你有帮助,请随手点赞,你的随手一个点赞是我前进的动力,谢谢阅读!

点赞是一种美德!!!!!!!!!!!!!
本文地址:https://blog.csdn.net/qq_33475202/article/details/103998984.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值