Spring Batch 输入资源必须存在(读取器处于“严格”模式)错误。

11

我使用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);
4个回答

10

我使用了来自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();
}

4

我曾经遇到过相同的问题,我使用了org.springframework.core.io.FileSystemResource类,就像这样: 文件路径: c:\data\geodata1.csv reader.setResource(new FileSystemResource(file));


4

刚刚找到了解决方案,使用 org.springframework.core.io.UrlResource; 类替换 org.springframework.core.io.ClassPathResource;


0

解决方案1:您可以按照以下方式使用PathResource

@Bean
public FlatFileItemReader<Person> reader() {
   new FlatFileItemReaderBuilder<Person>()
                .name("JobName")
                .resource(new PathResource(getReceivedFilePath("Location")))
                .targetType("Targetclass".class)
                .linesToSkip(" count of no of lines to skip")
                .delimited()
                .delimiter(recordSeparator)
                .includedFields(new Integer[] { 0, 1})
                .names(new String[] { "param1 in target class","param2 in target class etc" })
                .build();
}

你传递给PathResource的getReceivedFilePath应该如下所示,它可以获取任何以或结尾的文件等。

 public String getReceivedFilePath(String fileName) {

        File directory = new File(file.location);
        if (directory.isDirectory()) {
            File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith("extension.type"));
            for (File file : files) {
                if (FilenameUtils.getBaseName(file.getName()).startsWith(FilenameUtils.getBaseName(fileName))) {
                    return file.getAbsolutePath();
                }
            }
        }
        return directory.getName();
    }

解决方案2:您可以使用PathResource,但文件名应与您提供的路径完全匹配

例如:new PathResource("C:\files\filename.csv")


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接