从 Sftp 过滤并获取远程文件名列表

Filter and get list of remote file names from Sftp

我需要从修改时间超过某个给定时间的 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();

NLST 仅提取文件名,因此没有时间戳可供过滤。

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

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