@Bean
@StepScope
public MultiResourceItemReader<PosRow> multiResourceItemReader() {
MultiResourceItemReader<PosRow> resourceItemReader = new MultiResourceItemReader<>();
Resource[] resources = new Resource[0];
String path = "file:" + filePath + File.separator + filePattern + "*";
log.info("Looking for resource files matching {}", path);
try {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
resources = resolver.getResources(path);
} catch (IOException e) {
log.error("Problem with getting resource files ", e);
}
resourceItemReader.setResources(resources);
resourceItemReader.setDelegate(posRowReader());
return resourceItemReader;
}
即使位置中有文件,我也无法获得资源。在上一步中,文件被复制,然后我尝试使用PathMatchingResourcePatternResolver
查找文件。我在控制台上打印以下印刷:
c.s.p.p.batch.config.BatchConfiguration : Looking for resource files matching file:C:DevworkspacebatchsrcmainresourceslocalPathPositionFile*
o.s.b.item.file.MultiResourceItemReader : No resources to read. Set strict=true if this should be an error condition.
我可以看到位置模式已正确构造。
filePath
和filePattern
在application.properties
文件中看起来像这样:
positionFile.local-path=C:\Dev\workspace\batch\src\main\resources\localPath
positionFile.patternName=PositionFile
您未设置 classloader in pathmatchingresorcepatternresolver
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
我遇到了相同的问题,经过一些挖掘,似乎pathmatchingresourcepatternresolver有时会出现背部斜线解决的问题,有时有时可以正常工作
为了获得更高的可靠性,我解决了该问题,用前斜线代替了所有背部斜线
尝试以下操作:
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
path = path.replace("\", "/");
resources = resolver.getResources(path);
nb:file.separator在Windows Platform上也会发生问题