当前位置: 首页 > 面试题库 >

春季批处理:输入资源不存在类路径资源

方焱
2023-03-14
问题内容

我目前正在开发一个Spring
Batch,它可以在第一步中将Excel(.xsls)文件转换为CSV,然后读取CSV,对其进行处理并将其数据存储在数据库中。第一步效果很好。批处理在第二步停止,并发出以下警告:Input resource does not exist class path resource [C:/work/referentielAgenceCE.csv]。在我的代码之后:

spring-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

    <!-- import config Spring générale -->
    <import resource="classpath*:spring/***-batch-spring.xml" />

    <bean id="pathFichier" class="java.lang.String">
        <constructor-arg value="${batch.referentielAgenceCE.inputFilePathCSV}" />
    </bean>

    <batch:job id="simpleFileImportJob" xmlns="http://www.springframework.org/schema/batch">
        <batch:step id="convertStep" next="processingStep">
            <batch:tasklet ref="convert"/>
        </batch:step>
        <batch:step id="processingStep">
            <batch:tasklet>
                <batch:chunk reader="agenceCEReader" processor="agenceCEProcessor" writer="agenceCEWriter" 
                    commit-interval="5" />              
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="convert"
        class="com.***.referentielAgenceCE.convertTasklet.convertXLSXtoCVS" />

    <!-- Reader -->
    <bean id="agenceCEReader" scope="step"
        class="org.springframework.batch.item.file.FlatFileItemReader">     
        <property name="strict" value="false" />
        <property name="resource" ref="pathFichier" />
        <property name="linesToSkip" value="1" />
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value=";" />
                        <property name="names"
                            value="Date Création,Date Dernière modif.,Date Début Validité,Date Fin Validité,Caisse,Identifiant Agence,Libellé Agence,Type Agence,Libellé Type Agence,Téléphone,Fax,2EME Ligne Adresse,3EME Ligne Adresse,4EME Ligne Adresse,5EME Ligne Adresse,6EME Ligne Adresse,Pays,Ville,Identifiant Niv. 1,Type Niv. 1,Libellé Niv. 1,Identifiant Niv. 2,Type Niv. 2,Libellé Niv. 2,Identifiant Niv. 3,Type Niv. 3,Libellé Niv. 3,Identifiant Niv. 4,Type Niv. 4,Libellé Niv. 4,Identifiant Niv. 5,Type Niv. 5,Libellé Niv. 5,Identifiant Niv. 6,Type Niv. 6,Libellé Niv. 6,Identifiant Niv. 7,Type Niv. 7,Libellé Niv. 7,Identifiant Niv. 8,Type Niv. 8,Libellé Niv. 8,Identifiant Niv. 9,Type Niv. 9,Libellé Niv. 9,Identifiant Niv. 10,Type Niv. 10,Libellé Niv. 10,Identifiant Niv. 11,Type Niv. 11,Libellé Niv. 11,Identifiant Niv. 12,Type Niv. 12,Libellé Niv. 12,Identifiant Niv. 13,Type Niv. 13,Libellé Niv. 13,Identifiant Niv. 14,Type Niv. 14,Libellé Niv. 14,Jours/Heures Ouverture,Code Etat" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="com.***.referentielAgenceCE.mapping.AgenceCEFieldSetMapper" />
                </property>
            </bean>
        </property>
    </bean>

    <!-- Processor -->
    <bean id="agenceCEProcessor" 
        class="com.***.referentielAgenceCE.processor.AgenceCEItemProcessor"/>

    <!-- Writer -->
    <bean id="agenceCEWriter"
        class="com.***.referentielAgenceCE.writer.AgenceCEItemWriter" />
</beans>

第1步-convertXLSXtoCVS.java:

public class convertXLSXtoCVS implements Tasklet, InitializingBean{


    @Value("${batch.referentielAgenceCE.inputFilePathXLSX}")
    private String inputFile;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
            throws Exception {
        System.out.println("convertXLSXtoCVS.execute1");
        // For storing data into CSV files                
        StringBuffer data = new StringBuffer();                
        try {
            File outputFile = new File(getOutputFile(inputFile));
            FileWriter fw = new FileWriter(outputFile.getPath());

            BufferedWriter  fos = new BufferedWriter(fw);

            // Get the workbook object for XLSX file
            XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));

            // Get first sheet from the workbook
            XSSFSheet sheet = wBook.getSheetAt(0);
            Row row;
            Cell cell;

            // Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                row = rowIterator.next();
                // For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    cell = cellIterator.next();
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:                                                
                            data.append(cell.getBooleanCellValue() + ",");                                                
                            break;                                        
                        case Cell.CELL_TYPE_NUMERIC:                                                
                            data.append(cell.getNumericCellValue() + ",");                                                
                            break;                                        
                        case Cell.CELL_TYPE_STRING:                                                
                            data.append(cell.getStringCellValue() + ",");                                                
                            break;                                        
                        case Cell.CELL_TYPE_BLANK:                                                
                            data.append("" + ",");                                                
                            break;
                        default:                                                
                            data.append(cell + ",");
                    }
                }
            }
            fos.write(data.toString());
            fos.close();
        }catch (Exception ioe) {
            ioe.printStackTrace();
        }
        return RepeatStatus.FINISHED;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(inputFile, "inputFile must be set");

    }

    public String getOutputFile(String inputFile){
        String[] parts = inputFile.split("\\.");
        return parts[0]+"."+parts[1].replace("xlsx", "csv");
    }

}

我在属性文件中提到了文件路径,如下所示:

batch.referentielAgenceCE.inputFilePathXLSX=C\:\\work\\referentielAgenceCE.xlsx
batch.referentielAgenceCE.inputFilePathCSV=C\:\\work\\referentielAgenceCE.csv

当我在阅读器定义中从spring-config.xml删除时,出现以下错误:

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy44.open(Unknown Source)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:93)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:301)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:192)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:135)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:61)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:281)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:114)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:349)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574)
Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): URL [file://work//referentielAgenceCE.csv]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:250)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:134)
    ... 27 more

问题答案:

我找到了解决方案。我应该将’file:’添加到输出cvs文件路径中。所以应该是这样的:

batch.referentielAgenceCE.inputFilePathCSV=file\:C\:\\work\\referentielAgenceCE.csv instead of C\:\\work\\referentielAgenceCE.csv


 类似资料:
  • 问题内容: 我想以不在Bean中对Spring创建依赖项的方式注入类路径资源的URL。意味着,bean不应该使用Spring的接口/类。我怎样才能做到这一点? 问题答案: Spring能够将值隐式转换为: 。

  • 默认情况下,vue-loader 使用 css-loader 和 Vue 模版编译器自动处理样式和模版文件。在编译过程中,所有的资源路径例如 <img src="...">、background: url(...) 和 @import 会作为模块依赖。 例如,url(./image.png) 会被转换为 require('./image.png'),而 <img src="../image.png

  • 当 Vue Loader 编译单文件组件中的 <template> 块时,它也会将所有遇到的资源 URL 转换为 webpack 模块请求。 例如,下面的模板代码片段: <img src="../image.png"> 将会被编译成为: createElement('img', { attrs: { src: require('../image.png') // 现在这是一个模块的请

  • 我想设置csv文件的类路径资源。它位于我的项目资源文件夹中。当设置类路径并运行项目时,我得到的问题是类路径资源的运行时问题 result-match-metadata.csv文件存在于我的资源文件夹batchconfig.kt中 如何在批处理配置中设置csv文件路径

  • 上面是我的程序,错误显示为: 可能的问题是什么?请帮忙,因为我是新来的 我面临着同样的问题,我该如何解决?

  • 本文将介绍 Weex 中 uri(url) 的用法。包括使用图像、字体等资源,处理相对路径以及如何访问本地及打包的资源文件。 Schemes 本地资源 Weex SDK 提供 local scheme 来访问打包在应用程序中的资源,此 scheme 无法在 H5 环境下使用。目前,开发者可以在 image 组件和字体文件中使用本地资源。 在 iOS 中,Weex 会在 bundle resourc