【问题标题】:Filter and get list of remote file names from Sftp从 Sftp 过滤并获取远程文件名列表
【发布时间】:2021-03-10 10:50:32
【问题描述】:

我需要从 sftp 服务器获取修改时间超过给定时间的远程文件名列表,然后从 sftp 中删除所有这些文件,我使用SftpOutboundGatewayNLST 命令列出文件名字

但是它从给定的远程目录路径获取所有文件,我尝试了 .filter(lastModifiedFileListFilter) 不起作用然后我也尝试了 .filterFunction(logicGiven) 也不起作用

不知NLST命令是否支持过滤

这是代码示例:

过滤器实例:

private FileListFilter lastModifiedFilter(){
    LastModifiedFileListFilter fileListFilter = new LastModifiedFileListFilter();
    fileListFilter.setAge(120, TimeUnit.SECONDS);
    return fileListFilter;
}

使用 filter() 方法:

  @Bean
public IntegrationFlow deleteFiles(){
    return IntegrationFlows.from("integration.channel.bulk-delete")
            .handle(Sftp.outboundGateway(sftpSessionFactory(),
            AbstractRemoteFileOutboundGateway.Command.NLST,"headers[path]")
                    .filter(lastModifiedFilter()))
            .get();

使用 filterFunction() 方法:

 @Bean
public IntegrationFlow deleteFiles(){
    return IntegrationFlows.from("integration.channel.bulk-delete")
            .handle(Sftp.outboundGateway(sftpSessionFactory(),
            AbstractRemoteFileOutboundGateway.Command.NLST,"headers[path]")
                    .filterFunction(file->{
                        Instant decidedTime = Instant.now().minus(120, ChronoUnit.SECONDS);
                       return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
                    }))
            .log()
            .get();

如何过滤和获取文件名。我不想使用 mget 命令

更新

下一个句柄方法中的 RM 命令不会删除,我还在 doRm() 方法中放置了一个断点,尽管我的 for 循环中有文件,但它并没有停在那里

@Bean
public IntegrationFlow deleteFiles(){
    return IntegrationFlows.from("integration.channel.bulk-delete")
            .handle(Sftp.outboundGateway(sftpSessionFactory(),
            AbstractRemoteFileOutboundGateway.Command.LS,"headers[path]")
                    .options(AbstractRemoteFileOutboundGateway.Option.NAME_ONLY)
                    .filterFunction(file->{
                        Instant decidedTime = Instant.now().minus(120, ChronoUnit.SECONDS);
                        return Instant.ofEpochSecond(file.getAttrs().getMTime()).isBefore(decidedTime);
                    }))
            .log()
            .handle(fileList->{
                List<String> files = (List<String>) fileList.getPayload();
                for (String remoteFile: files) {
                    Sftp.outboundGateway(sftpSessionFactory(),
                            AbstractRemoteFileOutboundGateway.Command.RM,
                            "headers[file_remoteDirectory]+"+remoteFile);
                }
            })
            .get();

【问题讨论】:

    标签: spring-integration spring-integration-dsl spring-integration-sftp


    【解决方案1】:

    NLST 仅获取文件名,因此没有要过滤的时间戳。

    NLST 目前根本不应用任何过滤器。

    使用 LS(可能与递归 -R 选项一起使用)。

    【讨论】:

    • 谢谢。我已经解决了 LS 的问题,但是我有 RM 命令删除列出的文件,RM 命令不会删除任何文件。你能指出我在做什么错误吗,我已经更新了我的问题
    • 我看不出其中的任何一个是如何工作的,NAME_ONLY 返回一个字符串列表,而不是LsEntry 的列表。您的删除逻辑完全错误,您只是为每个文件名创建一个新的出站网关规范,而不是向它们发送任何内容;您需要添加.split(),然后添加handle(Sftp..)
    • 非常感谢,它成功了。在您提到之前,我对拆分器没有太多想法,只有我要做的就是让DefaultMessageSplitter 完成它的工作。再次感谢你们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多