使用 Spring Boot 项目使用 spring Integration 从 SFTP 服务器下载符合条件的文件

问题描述

我当前的项目基于 Spring Integration。我正在使用 spring Boot 开发这个项目。

我的目标是使用 Spring Integration 来完成以下任务。

  1. 连接到 SFTP

  2. 检查是否在本地的特定文件夹中创建了目录

  3. 检查特定于(CSV 和 XLSX)的文件的合格文件扩展名

  4. 从SFTP远程目录下载所有内容到本地目录,需要跟踪文件传输开始时间和文件传输结束时间。

  5. 逐行读取本地目录中的文件提取某些列信息。

你能给我一些建议吗?

我怎样才能获得转移开始时间? 注意:这个要求我必须开发为一个休息 api。请提供一些指导,我如何通过使用 spring 集成来实现这一目标?

谢谢。 :)

public class SftpConfig {

    @Value("${sftp.host}")
    private String sftpHost;

    @Value("${sftp.port:22}")
    private int sftpPort;

    @Value("${sftp.user}")
    private String sftpUser;

    @Value("${sftp.password:#{null}}")
    private String sftpPasword;

    @Value("${sftp.remote.directory:/}")
    private String sftpRemoteDirectory;

    @Value("${sftp.privateKey:#{null}}")
    private Resource sftpPrivateKey;

    @Value("${sftp.privateKeyPassphrase:}")
    private String privateKeyPassphrase;

    @Value("${sftp.remote.directory.download.filter:*.*}")
    private String sftpRemoteDirectoryDownloadFilter;

    @Value("${sftp.remote.directory.download:/}")
    private String sftpRemoteDirectoryDownload;

    @Value("${sftp.local.directory.download:${java.io.tmpdir}/localDownload}")
    private String sftpLocalDirectoryDownload;

    /*
     * The SftpSessionFactory creates the sftp sessions. This is where you define
     * the host,user and key information for your sftp server.
     */

    // Creating session for Remote Destination SFTP server Folder

    @Bean
    public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(sftpHost);
        factory.setPort(sftpPort);
        factory.setUser(sftpUser);
        if (sftpPrivateKey != null) {
            factory.setPrivateKey(sftpPrivateKey);
            factory.setPrivateKeyPassphrase(privateKeyPassphrase);
        } else {
            factory.setPassword("sftpPassword");
        }
        factory.setAllowUnkNownKeys(true);
        return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
    }
    
    /*
     * The SftpInboundFileSynchronizer uses the session factory that we defined above. 
     * Here we set information about the remote directory to fetch files from.
     * We Could also set filters here to control which files get downloaded
     */
    
    @Bean
    public SftpInboundFileSynchronizer SftpInboundFileSynchronizer () {
        SftpInboundFileSynchronizer synchronizer = new SftpInboundFileSynchronizer();       
        return null;
        
    }
    

解决方法

如果您查看 .highcharts-tooltip > span { padding: 10px; border: 2px solid red; border-radius: 30px 30px 30px 0; } 代替:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-streaming,加上使用 SftpStreamingMessageSource 逐行读取该文件(它也支持“first like as header”) ),您无需担心将文件传输到本地目录。您将按需处理远程内容。

另一方面,既然您谈论 REST API,您可能会有一些 FileSplitter 或 Spring Integration HTTP 入站网关:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-inbound,那么您需要考虑使用 { {1}} 和 @RestController 命令:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-outbound-gateway

如果您仍然需要跟踪每个文件的下载时间,您需要考虑使用该网关两次:使用 SftpOutboundGateway 命令和 MGET,第二次使用 {{1 }} 命令。此时,您可以为第二个网关的 LISTNAME_ONLY 通道添加 GET,因此您将有一个文件名信息来关联并捕获此网关前后的开始和停止时间.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...