首页 > 解决方案 > Spring SFTP过滤文件名根据当前日期

问题描述

我想用 Spring SFTP Adapters 读取文件。该文件(名称:“myFile_20210510.csv”)每天在 SFTP 上创建。这是代码:

  @Bean
  @InboundChannelAdapter(channel = "stream", poller = @Poller(fixedDelay = "1000"))
  public MessageSource<InputStream> ftpMessageSource() {
    SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template());
    messageSource.setRemoteDirectory(sftpDirectory);
    messageSource.setLoggingEnabled(true);
    messageSource.setFilter(new SftpRegexPatternFileListFilter(".*myFile_"
        + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")).toString() + "\\.csv$"));
    return messageSource;
  }

到目前为止,这有效。如果我启动应用程序并将文件复制到 SFTP 中,它就会被读取。问题是,根据Spring SFTP 不同的文件名正则表达式 ,过滤器被设置为运行时对象,因此过滤器在我启动应用程序的那天固定,并且不会每天更新。最后,只有在我重新启动应用程序后才能读取文件。

那么,我该如何设置过滤器,以实现每天正确设置日期呢?

谢谢您的帮助。

问候,菲尔

标签: springfiltersftp

解决方案


推荐阅读