我正在使用Spring Batch MultiResourceItemReader来读取多文件文件。这些文件位于父目录及其子目录中。
已经尝试:
通过我自己的自定义代码读取文件,然后手动创建Resource数组。
使用PathMatchingResourcePatternResolver,如代码示例所示(受此Finding Resources with PathMatchingResourcePatternResolver and URLClassloader in JARs启发
@Bean
public MultiResourceItemReader<List<SingleJsonRowInput>>
multiResourceItemReader() {
PathMatchingResourcePatternResolver patternResolver = new
PathMatchingResourcePatternResolver();
Resource resources[] = null;;
try {
resources =
patternResolver.getResources("file:C:\\inputFolder\\**\\*.json");
} catch (IOException e) {
e.printStackTrace();
}
MultiResourceItemReader<List<SingleJsonRowInput>>
multiResourceItemReader = new MultiResourceItemReader<>();
multiResourceItemReader.setResources(resources);
multiResourceItemReader.setDelegate(new
ItemReaderForMulti(fileManager));
return multiResourceItemReader;
}
答案 0 :(得分:1)
您可以使用以下代码段:
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("file:/root/folder/**/*.json");
**/*
将从root/folder
递归返回文件。然后,您将资源数组传递到MultiResourceItemReader
。
答案 1 :(得分:0)
代替使用Windows反斜杠,解决方案是使用Unix \ Linux之类的语法:
无效: 资源= patternResolver.getResources(“ file:C:\\ inputFolder \\ ** \\ *。json”);
效果很好: 资源= patternResolver.getResources(“ file:C:/ inputFolder / ** / *。json”);