【问题标题】:Spring Integration (SFTP): problem with downloading files from subdirectoriesSpring Integration (SFTP):从子目录下载文件的问题
【发布时间】:2021-06-06 21:12:29
【问题描述】:

我在 Spring Boot 应用程序中使用 Spring Integration 5.4.4 版。 我需要从“server_sftp”目录下的子目录中获取所有 XML 文件。为此,我使用带有 mget 命令的 SFTP Streaming Inbound Channel Adapter 和 SFTP Outbound Gateway。不幸的是,该应用程序仅从根目录(“server_sftp”)下载文件,而不从子目录下载文件。

我哪里出错了?

@Bean
@InboundChannelAdapter(channel = "downloadXmlFileInputChannel", poller = @Poller(fixedDelay = "300000"))
public MessageSource<InputStream> sftpXmlFileMessageSource() {
    SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template());
    messageSource.setRemoteDirectory("server_sftp/");
    return messageSource;
}

@Bean
public IntegrationFlow xmlFilesReadingFlow() {
    return IntegrationFlows
            .from(sftpXmlFileMessageSource(), e -> e.poller(Pollers.fixedDelay(Duration.ofSeconds(5))))
            .handle(Sftp
                    .outboundGateway(template(), Command.MGET, "'server_sftp/*'")
                    .options(Option.RECURSIVE)
                    .autoCreateLocalDirectory(true)
                    .localDirectoryExpression("'../webapps/event_report_app-1.0/xmlFilesLocalDirectory/' + #remoteDirectory")
                    .localFilenameExpression("#remoteFileName.replaceFirst('sftpSource', 'localTarget')"))
            .channel("downloadXmlFileOutputChannel")
            .get();
}

@Bean
public PollableChannel downloadXmlFileInputChannel() {
    return new QueueChannel();
}

@Bean
public DirectChannel downloadXmlFileOutputChannel() {
    return new DirectChannel();
}

directory structure on the sftp server

【问题讨论】:

    标签: spring-integration spring-integration-sftp


    【解决方案1】:

    使用带有递归 MGET 命令的 outbound gateway 来获取完整的树。

    使用 mget 命令 mget 根据模式检索多个远程文件,并支持以下选项:

    -P:保留远程文件的时间戳。

    -R:递归检索整个目录树。

    -x:如果没有文件与模式匹配,则抛出异常(否则返回空列表)。

    -D:传输成功后删除每个远程文件。如果忽略传输,则不会删除远程文件,因为 FileExistsMode 为 IGNORE 并且本地文件已经存在。

    mget 操作产生的消息负载是一个 List 对象(即一个 File 对象的 List,每个对象代表一个检索到的文件)。

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多