基于Spring 的 FTP 下载

FTP 下载有多种方式,一般都是基于 apache 的 FTPClient 的操作,当然JDK1.8之后也提供了支持。

这里记录一下 spring-integration-ftp (基于apache 的 FTPClient),

支持中文

支持并发

参考spring ftp文档

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-ftp</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
    // 依赖注入
    @Autowired
    private FtpRemoteFileTemplate ftpRemoteFileTemplate;

    @GetMapping("/test")
    public ResponseEntity<String> test() {
        String filePath = "/IB业务发展部/test_20180731_11.mp4";

        // 判断是否存在
        boolean exists = ftpRemoteFileTemplate.exists(filePath);

        // 下载文件
        ftpRemoteFileTemplate.get(filePath, (inputStream -> {
            try (FileOutputStream fos = new FileOutputStream("C:/Users/test/AppData/Local/Temp/download/" + filePath)) {
                IOUtils.copy(inputStream, fos);
            }
        }));


        return ResponseEntity.ok("启动job成功");
    }

 

# ftp 的配置文件(yaml格式)。
ftp:
  host: 12.12.12.12
  port: 8848
  username: test
  password: test
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.ftp.session.DefaultFtpSessionFactory;
import org.springframework.integration.ftp.session.FtpRemoteFileTemplate;

/**
 * @author Fuyuanwu
 * @date 2019/7/31 10:01
 */
@Configuration
public class FTPConfig {
    @Bean("ftpSessionFactory")
    public SessionFactory<FTPFile> ftpSessionFactory(FtpSessionFactoryConfig config) {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory() {
            /**
             * 高并发下和ftp服务器握手会返回response 421 received.错误。因此采用同步。
             * @return
             */
            @Override
            public synchronized FtpSession getSession() {
                return super.getSession();
            }
        };
        sf.setHost(config.getHost());
        sf.setPort(config.getPort());
        sf.setUsername(config.getUsername());
        sf.setPassword(config.getPassword());
        sf.setControlEncoding("UTF-8");
        sf.setFileType(FTP.BINARY_FILE_TYPE);
        sf.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
        
        CachingSessionFactory<FTPFile> csf = new CachingSessionFactory<>(sf);
        csf.setPoolSize(config.getPoolSize());

        return csf;
    }

    @Bean
    @ConfigurationProperties(prefix = "ftp")
    public FtpSessionFactoryConfig ftpSessionFactoryProperties() {
        return new FtpSessionFactoryConfig();
    }

    @Bean
    public FtpRemoteFileTemplate ftpRemoteFileTemplate(@Qualifier("ftpSessionFactory") SessionFactory<FTPFile> sessionFactory) {
        FtpRemoteFileTemplate ftpRemoteFileTemplate = new FtpRemoteFileTemplate(sessionFactory);
        ftpRemoteFileTemplate.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST);

        return ftpRemoteFileTemplate;
    }

   class FtpSessionFactoryConfig {
        private String host;
        private int port;
        private String username;
        private String password;
        private int poolSize = 5;
        private int dataTimeout = 1000 * 60 * 10;

        public int getDataTimeout() {
            return dataTimeout;
        }

        public void setDataTimeout(int dataTimeout) {
            this.dataTimeout = dataTimeout;
        }

        public int getPoolSize() {
            return poolSize;
        }

        public void setPoolSize(int poolSize) {
            this.poolSize = poolSize;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值