为什么我的 PathResource 不可读?

Why is my PathResource not readable?

我尝试编写一个 Spring 读取 csv 文件的批处理应用程序 并将其内容存储在数据库中。我无法创建 一个 FlatFileItemReader 因为我得到一个 IllegalStateException 说明 输入资源必须是可读的 (reader 处于 'strict' 模式).

这是我对 reader 的配置:

@Bean
public ItemReader<CadSystem> cadSystemReader(final Path backupDirectory,
        final CadSystemFieldSetMapper fieldSetMapper) {
    final FlatFileItemReader<CadSystem> reader = new FlatFileItemReader<>();
    final DefaultLineMapper<CadSystem> lineMapper = new DefaultLineMapper<>();
    final PathResource resource = new PathResource(backupDirectory.resolve("cad_systems.csv"));

    lineMapper.setLineTokenizer(new DelimitedLineTokenizer(";"));
    lineMapper.setFieldSetMapper(fieldSetMapper);
    reader.setResource(resource);
    reader.setLineMapper(lineMapper);

    return reader;
}

这是堆栈跟踪:

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:386)
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:135)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:210)
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:227)
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:121)
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:115)
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at App.main(App.java:11)
Caused by: java.lang.IllegalStateException: Input resource must be readable (reader is in 'strict' mode): path [D:\backup\cad_systems.csv]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:259)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
    ... 20 common frames omitted

该文件存在于 D:\backup\cad_systems.csv 并且所有 users/groups 都可读。

导致此错误的原因是什么?

更新

我试过这样使用 FileSystemResource

final FileSystemResource resource = new FileSystemResource(backupDirectory.resolve("cad_systems.csv").toFile());

现在可以了。但是 PathResource 有什么问题?

错误是由 bug in the JDK 7 引起的。使用 Java 8.

时不会出现此问题