FtpInboundFileSynchronizer – 设置的过滤器未按预期工作

huangapple go评论104阅读模式
英文:

FtpInboundFileSynchronizer - setting filter not working as expected

问题

以下是翻译好的内容:

我有一个子方法,用于构建我的FtpInboundFileSynchronizer对象(从超出范围的工厂对象生成)。

  1. private FtpInboundFileSynchronizer createFtpInboundFileSynchronizer(SessionFactory<FTPFile> factory) {
  2. var synchronizer = new FtpInboundFileSynchronizer(factory);
  3. CompositeFileListFilter filter = new CompositeFileListFilter<>();
  4. filter.addFilter(new FtpSimplePatternFileListFilter("filename1.txt"));
  5. filter.addFilter(new FtpSimplePatternFileListFilter("filename2.txt"));
  6. synchronizer.setFilter(filter);
  7. synchronizer.setRemoteDirectory(ftpConfiguration.getPath());
  8. synchronizer.setDeleteRemoteFiles(false);
  9. return synchronizer;
  10. }

然而,在这种情况下,我获取不到任何文件。如果我移除其中一个FTPSimplePatternFileListFilter实例,它可以正确地从FTP服务器检索出一个文件名。

目标是仅下载预定义列表中的完整文件名,包括名称和扩展名。
我似乎无法弄清楚如何做到这一点。有人能帮忙吗?

英文:

I have a sub-method that builds my FtpInboundFileSynchronizer object. (from a factory object generated out of scope)

  1. private FtpInboundFileSynchronizer createFtpInboundFileSynchronizer(SessionFactory&lt;FTPFile&gt; factory) {
  2. var synchronizer = new FtpInboundFileSynchronizer(factory);
  3. CompositeFileListFilter filter = new CompositeFileListFilter&lt;&gt;();
  4. filter.addFilter(new FtpSimplePatternFileListFilter(&quot;filename1.txt&quot;));
  5. filter.addFilter(new FtpSimplePatternFileListFilter(&quot;filename2.txt&quot;));
  6. synchronizer.setFilter(filter);
  7. synchronizer.setRemoteDirectory(ftpConfiguration.getPath());
  8. synchronizer.setDeleteRemoteFiles(false);
  9. return synchronizer;
  10. }

However, in this case, I get no files. If I remove one of the FTPSimplePAtternFileListFilter instances, it correctly retrieves that one filename from the FTP server.

The aim is to only download a predefined list of full file names, so both name and extension.
I cannot seem to wrap my head around how to do this. Anyone able to help?

答案1

得分: 0

  1. import java.util.Arrays;
  2. import org.apache.commons.net.ftp.FTPFile;
  3. import org.springframework.integration.file.filters.AbstractDirectoryAwareFileListFilter;
  4. import org.springframework.util.AntPathMatcher;
  5. /**
  6. * 接受特定文件名列表的过滤器。
  7. */
  8. public class MultiPatternFileListFilter extends AbstractDirectoryAwareFileListFilter<FTPFile> {
  9. private final AntPathMatcher matcher = new AntPathMatcher();
  10. private String[] patterns;
  11. public MultiPatternFileListFilter(String... patterns) {
  12. this.patterns = patterns;
  13. }
  14. @Override
  15. public boolean accept(FTPFile file) {
  16. return alwaysAccept(file) || (file != null && this.matchesAnyPattern(file));
  17. }
  18. @Override
  19. protected boolean isDirectory(FTPFile file) {
  20. return false;
  21. }
  22. protected String getFilename(FTPFile file) {
  23. return (file != null) ? file.getName() : null;
  24. }
  25. private boolean matchesAnyPattern(FTPFile file) {
  26. return Arrays.stream(patterns).anyMatch(pattern -> this.matcher.match(pattern, this.getFilename(file)));
  27. }
  28. }

这解决了我的问题。

英文:
  1. import java.util.Arrays;
  2. import org.apache.commons.net.ftp.FTPFile;
  3. import org.springframework.integration.file.filters.AbstractDirectoryAwareFileListFilter;
  4. import org.springframework.util.AntPathMatcher;
  5. /**
  6. * Filter to accept specific file name list.
  7. */
  8. public class MultiPatternFileListFilter extends AbstractDirectoryAwareFileListFilter&lt;FTPFile&gt; {
  9. private final AntPathMatcher matcher = new AntPathMatcher();
  10. private String[] patterns;
  11. public MultiPatternFileListFilter(String... patterns) {
  12. this.patterns = patterns;
  13. }
  14. @Override
  15. public boolean accept(FTPFile file) {
  16. return alwaysAccept(file) || (file != null &amp;&amp; this.matchesAnyPattern(file));
  17. }
  18. @Override
  19. protected boolean isDirectory(FTPFile file) {
  20. return false;
  21. }
  22. protected String getFilename(FTPFile file) {
  23. return (file != null) ? file.getName() : null;
  24. }
  25. private boolean matchesAnyPattern(FTPFile file) {
  26. return Arrays.stream(patterns).anyMatch(pattern -&gt; this.matcher.match(pattern, this.getFilename(file)));
  27. }
  28. }

This solved my problem.

huangapple
  • 本文由 发表于 2020年4月8日 21:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61101822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定