0
   @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:\Dev\workspace\batch\src\main\resources\localPath\PositionFile*
o.s.b.item.file.MultiResourceItemReader  : No resources to read. Set strict=true if this should be an error condition.

我可以看到 locationPattern 构造正确。

filePath和在文件中filePattern看起来像这样:application.properties

positionFile.local-path=C:\\Dev\\workspace\\batch\\src\\main\\resources\\localPath

positionFile.patternName=PositionFile
4

2 回答 2

0

您没有 在PathMatchingResourcePatternResolver中设置ClassLoader

ClassLoader cl = this.getClass().getClassLoader();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
于 2018-02-15T07:22:36.893 回答
0

我遇到了同样的问题,经过一番挖掘,似乎 PathMatchingResourcePatternResolver 有时会用反斜杠解决问题,有时工作正常

为了提高可靠性,我解决了用正斜杠替换所有反斜杠的问题

尝试这个:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
path = path.replace("\\", "/");
resources = resolver.getResources(path);

注意:Windows 平台上的 File.separator 也会出现问题

于 2021-03-25T08:47:23.660 回答