java - Spring batch Input resource must exist (reader is in 'strict' mode)错误

标签 java spring spring-batch

我使用 Spring Batch 来解析 csv 文件。当文件在资源目录中时效果很好,但在其他地方不起作用。我收到糟糕的错误

Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): class path resource [c:/data/geodata1.csv]

我的代码

spring.datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:mydb;MODE=Oracle
server:
  port: 9001
geodata:
  file: c:/data/geodata1.csv

@Value("${geodata.file}")
private String filePath;

@Bean
public FlatFileItemReader<Person> reader() {
    FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
    reader.setResource(new ClassPathResource(filePath));
    reader.setLineMapper(new DefaultLineMapper<Person>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[] {"clientId", "longitude", "latitude", });
        }});
        setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
            setTargetType(Person.class);
        }});
    }});
    return reader;
}

但是这段代码效果很好

File file = new File(filePath);

最佳答案

使用 org.springframework.core.ioPathResource ,它对我有用

@Bean
@StepScope
public FlatFileItemReader<CourseCountry> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
    return new FlatFileItemReaderBuilder<CourseCountry>()
      .name("studentItemReader")        
      .resource(new PathResource(pathToFile))
      .lineMapper(lineMapper())
      .linesToSkip(1)
      .build();
}

关于java - Spring batch Input resource must exist (reader is in 'strict' mode)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46403159/

相关文章:

node.js - 如何在本地运行的 Web 服务之间进行通信

带有持久存储作业注册表的 spring batch 重新启 Action 业

java - 使用java进行AES解密

java - 验证信用卡详细信息

spring - 使用 spring-security 更改方法调用的安全上下文

mysql - 使用 flyway 时在启动时清除数据库

java - SimpleAsyncTaskExecutor 尝试在处理完成后读取记录

java - 如何在 Spring Batch 应用程序中设置退出状态

java - 使用JAVA合并多个XML文件的不同节点

Java对不包括特定值的整数列表进行排序