java Spring批处理输入资源必须存在(读者处于“严格”模式)错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46403159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:12:12  来源:igfitidea点击:

Spring batch Input resource must exist (reader is in 'strict' mode) error

javaspringspring-batch

提问by ttt

I use Spring Batch for parse csv file. It works great, when file in resource directory, but doesn't work from another place. I get suck error

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

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

My code

我的代码

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;
}

But this code works good

但是这段代码效果很好

File file = new File(filePath);

采纳答案by ttt

Just found the solution use org.springframework.core.io.UrlResource;class instead of org.springframework.core.io.ClassPathResource;

刚刚发现解决方案使用org.springframework.core.io.UrlResource;类而不是org.springframework.core.io.ClassPathResource;

回答by zedtimi

I had find the same problem and i'd use org.springframework.core.io.FileSystemResource class like this: file: c:\data\geodata1.csv reader.setResource(new FileSystemResource(file));

我发现了同样的问题,我会像这样使用 org.springframework.core.io.FileSystemResource 类: file: c:\data\geodata1.csv reader.setResource(new FileSystemResource(file));