FtpConfiguration.java 2.16 KB
package com.viontech.configuration;

import com.viontech.ftp.FTPClientFactory;
import com.viontech.ftp.FTPClientHelper;
import com.viontech.ftp.FTPClientPool;
import com.viontech.ftp.FtpPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FtpConfiguration {

    @Value("${ftp.host:127.0.0.1}")
    private String host;
    @Value("${ftp.port:21}")
    private int port;
    @Value("${ftp.username:}")
    private String username;
    @Value("${ftp.password:}")
    private String password;
    @Value("${ftp.tempPath:}")
    private String tempPath;

    @Bean("ftpPoolConfig")
    @ConditionalOnProperty(name = "ftp.host")
    public FtpPoolConfig ftpPoolConfig() {
        FtpPoolConfig ftpPoolConfig = new FtpPoolConfig();
        ftpPoolConfig.setHost(host);
        ftpPoolConfig.setPort(port);
        ftpPoolConfig.setUsername(username);
        ftpPoolConfig.setPassword(password);
        //ftpPoolConfig.setMaxWaitMillis(1000);
        return ftpPoolConfig;
    }

    @Bean("ftpClientFactory")
    @ConditionalOnBean(FtpPoolConfig.class)
    public FTPClientFactory ftpClientFactory(FtpPoolConfig ftpPoolConfig) {
        FTPClientFactory ftpClientFactory = new FTPClientFactory();
        ftpClientFactory.setFtpPoolConfig(ftpPoolConfig);
        return ftpClientFactory;
    }

    @Bean("ftpClientPool")
    @ConditionalOnBean(FTPClientFactory.class)
    public FTPClientPool ftpClientPool(FTPClientFactory ftpClientFactory) {
        return new FTPClientPool(ftpClientFactory);
    }

    @Bean("ftpClientHelper")
    @ConditionalOnBean(FTPClientPool.class)
    public FTPClientHelper ftpClientHelper(FTPClientPool ftpClientPool) {
        FTPClientHelper ftpClientHelper = new FTPClientHelper();
        ftpClientHelper.setFtpClientPool(ftpClientPool);
        if (!tempPath.trim().isEmpty())
            ftpClientHelper.setTempPath(tempPath);
        return ftpClientHelper;
    }

}