英文:
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<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;
}
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<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)));
}
}
This solved my problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论