为什么我的PathResource不可读?

时间:2015-04-29 07:32:10

标签: java spring spring-batch

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

这是我对读者的配置:

@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$1.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,并且对所有用户/组都可读。

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

更新

我尝试使用FileSystemResource这样的

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

现在它有效。但是PathResource

出了什么问题

1 个答案:

答案 0 :(得分:1)

错误是由bug in the JDK 7引起的。使用Java 8时不会发生此问题。