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

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

FtpInboundFileSynchronizer - setting filter not working as expected

问题

以下是翻译好的内容:

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

private FtpInboundFileSynchronizer createFtpInboundFileSynchronizer(SessionFactory<FTPFile> factory) {
    var synchronizer = new FtpInboundFileSynchronizer(factory);
    CompositeFileListFilter filter = new CompositeFileListFilter<>();
    filter.addFilter(new FtpSimplePatternFileListFilter("filename1.txt"));
    filter.addFilter(new FtpSimplePatternFileListFilter("filename2.txt"));
    synchronizer.setFilter(filter);
    synchronizer.setRemoteDirectory(ftpConfiguration.getPath());
    synchronizer.setDeleteRemoteFiles(false);
    return synchronizer;
}

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

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

英文:

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

private FtpInboundFileSynchronizer createFtpInboundFileSynchronizer(SessionFactory&lt;FTPFile&gt; factory) {
        var synchronizer = new FtpInboundFileSynchronizer(factory);
        CompositeFileListFilter filter = new CompositeFileListFilter&lt;&gt;();
        filter.addFilter(new FtpSimplePatternFileListFilter(&quot;filename1.txt&quot;));
        filter.addFilter(new FtpSimplePatternFileListFilter(&quot;filename2.txt&quot;));
        synchronizer.setFilter(filter);
        synchronizer.setRemoteDirectory(ftpConfiguration.getPath());
        synchronizer.setDeleteRemoteFiles(false);
        return synchronizer;
    }

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

import java.util.Arrays;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.integration.file.filters.AbstractDirectoryAwareFileListFilter;
import org.springframework.util.AntPathMatcher;

/**
 * 接受特定文件名列表的过滤器。
 */
public class MultiPatternFileListFilter extends AbstractDirectoryAwareFileListFilter<FTPFile> {

    private final AntPathMatcher matcher = new AntPathMatcher();
    private String[] patterns;

    public MultiPatternFileListFilter(String... patterns) {
        this.patterns = patterns;
    }

    @Override
    public boolean accept(FTPFile file) {
        return alwaysAccept(file) || (file != null && this.matchesAnyPattern(file));
    }

    @Override
    protected boolean isDirectory(FTPFile file) {
        return false;
    }

    protected String getFilename(FTPFile file) {
        return (file != null) ? file.getName() : null;
    }

    private boolean matchesAnyPattern(FTPFile file) {
        return Arrays.stream(patterns).anyMatch(pattern -> this.matcher.match(pattern, this.getFilename(file)));
    }
}

这解决了我的问题。

英文:
import java.util.Arrays;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.integration.file.filters.AbstractDirectoryAwareFileListFilter;
import org.springframework.util.AntPathMatcher;

/**
 * Filter to accept specific file name list.
 */
public class MultiPatternFileListFilter extends AbstractDirectoryAwareFileListFilter&lt;FTPFile&gt; {

    private final AntPathMatcher matcher = new AntPathMatcher();
    private String[] patterns;

    public MultiPatternFileListFilter(String... patterns) {
        this.patterns = patterns;
    }

    @Override
    public boolean accept(FTPFile file) {
        return alwaysAccept(file) || (file != null &amp;&amp; this.matchesAnyPattern(file));
    }

    @Override
    protected boolean isDirectory(FTPFile file) {
        return false;
    }

    protected String getFilename(FTPFile file) {
        return (file != null) ? file.getName() : null;
    }

    private boolean matchesAnyPattern(FTPFile file) {
        return Arrays.stream(patterns).anyMatch(pattern -&gt; this.matcher.match(pattern, this.getFilename(file)));
    }
}

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:

确定