【问题标题】:Spring Integration Ftp | how to move remote files into another directory in remote server after FTP fetch completeSpring 集成 Ftp | FTP获取完成后如何将远程文件移动到远程服务器中的另一个目录
【发布时间】:2022-08-08 20:26:31
【问题描述】:

我们有一个远程 FTP 服务器,其中有一个文件夹 \"test/\",其中包含某些文本文件。 \"test/\" 文件夹内有另一个子目录 \"archive/\"。

FTP服务器->
-测试/
---abc.txt
---xyz.txt
---档案/

我们可以通过本地目录中的 Spring Integration 流下载所有文本文件。 现在我们正在研究如何在 FTP 服务器本身的文件夹 \"archive\" 中移动远程文本文件,一旦它被下载到本地。

我们正在尝试在这样的 handle() 方法中执行此操作->

@Bean
public IntegrationFlow integrationFlow() {


    File localDirectory = new File(\"tmp/\");

    FtpInboundChannelAdapterSpec ftpInboundChannelAdapterSpec = Ftp.inboundAdapter(gimmeFactory())
            .remoteDirectory(\"test/\")
            .autoCreateLocalDirectory(true)
            .regexFilter(\".*\\\\.txt$\")
            .localDirectory(localDirectory)
            .preserveTimestamp(true)
            .remoteFileSeparator(\"/\");

    return IntegrationFlows.from(ftpInboundChannelAdapterSpec, pc -> pc.poller(pm -> pm.fixedRate(1000, TimeUnit.MILLISECONDS)))
            .handle((file, messageHeaders) -> {
                messageHeaders.forEach((k, v) -> System.out.println(k + \':\' + v));
                return null;
            })
            .handle(Ftp.outboundGateway(gimmeFactory(), AbstractRemoteFileOutboundGateway.Command.MV, \"\'test/archive\'\"))
            .get();

}

但它没有移动到远程“存档”文件夹位置。 我们不太确定如何以任何其他方式处理此操作。 我们可以做些什么来修复上面的代码 sn-p 或做一些不同的事情来实现我们想要的吗? 请指教。

更新

谢谢加里的指点。
我能够通过按照以下代码 sn-p-> 中给出的方式解决问题

@Bean
    public IntegrationFlow integrationFlow() {            
        File localDirectory = new File(\"tmp/\");
        FtpInboundChannelAdapterSpec ftpInboundChannelAdapterSpec = Ftp.inboundAdapter(gimmeFactory())
                .remoteDirectory(\"test/\")
                .autoCreateLocalDirectory(true)
                .regexFilter(\".*\\\\.txt$\")
                .localDirectory(localDirectory)
                .preserveTimestamp(true)
                .remoteFileSeparator(\"/\");
        
        return IntegrationFlows
                .from(ftpInboundChannelAdapterSpec, e -> e.poller(Pollers.fixedDelay(Duration.ofSeconds(5))))
                .handle(Ftp.outboundGateway(gimmeFactory(), AbstractRemoteFileOutboundGateway.Command.LS, \"\'test/\'\")
                        .options(AbstractRemoteFileOutboundGateway.Option.NAME_ONLY))
                .split()
                .handle(Ftp.outboundGateway(gimmeFactory(), AbstractRemoteFileOutboundGateway.Command.MV, \"\'test/\' +payload\").renameExpression(\"\'test/archive/\' +payload\"))
                .channel(\"nullChannel\")
                .get();
}

    标签: spring-integration spring-integration-dsl spring-integration-ftp


    【解决方案1】:

    由于您从第一个.handle() 返回null,因此流程停止在那里;您需要返回包含from 路径的有效负载,并且需要rename-expression 来指定to 路径。

    https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#using-the-mv-command

    mv 命令移动文件。

    mv 命令没有选项。

    expression 属性定义“from”路径,rename-expression 属性定义“to”路径。默认情况下,重命名表达式是 headers['file_renameTo']。此表达式的计算结果不得为 null 或空字符串。如有必要,将创建任何必要的远程目录。结果消息的有效负载是 Boolean.TRUE。 file_remoteDirectory 标头提供原始远程目录,file_remoteFile 标头提供文件名。新路径位于 file_renameTo 标头中。

    从版本 5.5.6 开始,为方便起见,可以在 mv 命令中使用 remoteDirectoryExpression。如果“from”文件不是完整的文件路径,remoteDirectoryExpression 的结果将用作远程目录。这同样适用于“to”文件,例如,如果任务只是重命名某个目录中的远程文件。

    【讨论】:

      【解决方案2】:

      如何测试上面的工作代码,有人可以在这里帮忙吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-06
        • 1970-01-01
        • 2015-07-21
        相关资源
        最近更新 更多