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

2023-12-12

我目前正在开发一个 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 步 - 将XLSX转换为CVS.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:”添加到输出 csv 文件路径。所以应该是这样的:

batch.referentielAgenceCE.inputFilePathCSV=file\:C\:\\work\\referentielAgenceCE.csv instead of C\:\\work\\referentielAgenceCE.csv
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring批处理:输入资源不存在类路径资源 的相关文章

随机推荐

  • 为单个图像创建多个蒙版

    我正在尝试创建以下场景 并且我愿意使用 jquery css 和 html 的任意组合 不过我现在想远离 HTML 5 但我愿意看看它如果这是唯一的解决方案 我想要一张大图像 只能在蒙版所在的位置可见 我尝试了多种技术 但都不起作用 任何建
  • 解析输出并计算字符串出现的次数[关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 我正在运行一些代码 由于复杂性和篇幅 我想也许可以使用一些代码来让我的生活变得轻松 所以代码运行时使用 gt commandA output results are popping
  • Jenkins:凭证中私钥的正确格式是什么

    我正在 Windows Server 2016 上运行的 Jenkins 2 152 中创建一个作业 该作业需要从 bitbucket org 上托管的 git 存储库中提取 我通过 git bash 测试了 ssh 密钥 所以我知道它有效
  • 将 C# winforms 按钮处理为击键?

    I want to make a button on that when pressed the key combination Ctrl is pressed and another where Ctrl is pressed How c
  • 有没有办法让 Behat 不会因 PHP 通知错误而失败?

    我知道最好的做法是定义所有变量并在评估之前检查数组索引 但是 我正在尝试对在一些尚未以这种方式编码的遗留代码之上开发的新功能运行一些测试 Behat 失败并显示以下消息 Scenario Add a new resource feature
  • var fn = function() {...} 和 var fn = function foo() {...} 有不同吗?

    当您将函数分配给变量时 如果使用命名函数而不是匿名函数 这有什么区别吗 以下生成错误 foo 未定义 var fn function foo foo 谁能澄清这里发生了什么事吗 你正在创建一个命名函数表达式 IE 中除外 该名称仅在函数内部
  • JSP - 在 JSP 页面之间传递参数

    如何使用纯 Java 代码在 JSP 页面之间传递参数 IE 我不想使用如下代码
  • 如何在Polars中按数据类型选择列?

    在 pandas 中我们有pandas DataFrame select dtypes根据选择某些列的方法dtype 在 Polars 中是否有类似的方法来做这样的事情 可以将数据类型传递给pl col import polars as p
  • 如何多行匹配两个字符串之间的所有文本

    我正在努力完成与所见相同的事情here 即假设您有如下文本 p something p p class sdf some text p p some other text p p The end p 匹配的正则表达式是什么 p class
  • 如何将 NodeJS 与 Angular 连接(在 Nginx 中)

    我有一个带有 Angular 和 NodeJS 的存储库 我在詹金斯中表现 install globally npm install g bower npm install g gulp install bower install npm
  • 如何在 VB 6 中获取当前 CPU 和 RAM 使用情况?

    如何获取VB 6代码中的CPU和内存使用情况 谢谢 确定当前机器上的CPU使用率
  • 从文本文件读取数据并创建对象

    我需要一些帮助 我正在 Java 上进行超市模拟 但我遇到了一个问题 我有一个文本文件 Stock txt 其中包含超市的所有库存 例如 0 面包店 巧克力蛋糕 12 5 250 1 肉 优质牛排 2 6 120 2 海鲜 金枪鱼 1 2
  • Java两次之间的差异[重复]

    这个问题在这里已经有答案了 可能的重复 计算两个 Java 日期实例之间的差异 时间 1 17 05 时间 2 17 08 我想在几秒钟内得到差异 long diffInMillis newerDate getTime olderDate
  • 在 Pig 中提取 CSV 文件的第一行

    我有几个 CSV 文件 标题始终是文件中的第一行 在 Pig 中将该行作为字符串从 CSV 文件中取出的最佳方法是什么 不能使用 sed awk 等进行预处理 我尝试使用常规 PigStorage 和 Piggybank CsvLoader
  • JavaScript |运算符[重复]

    这个问题在这里已经有答案了 谁能解释一下什么是 和之后的值呢 我知道 0 的输出会创建 13 个集合 即数字 3 2 1 0 但是 呢 1 或 2 var i 52 while i alert i 13 0 它是按位或运算符 有解释和例子在
  • 如何将 java 代码嵌入到批处理脚本中?是否可以创建 .java/.bat 混合文件?

    虽然有一些技术可以让您创建perfect 并非如此perfect 批处理文件与一些 本机 Windows 脚本语言的混合体 完美 的混合体应该是什么样子 嵌入的代码必须可以按原样使用 并且您应该有能力 将其复制粘贴到您想要的任何其他编辑器
  • 虚拟基类的创建顺序

    我有以下问题 struct A1 A1 std cout lt lt A1 struct A2 A2 std cout lt lt A2 struct AA1 virtual A1 A2 AA1 std cout lt lt AA1 str
  • 使用 Cobertura 从代码覆盖率中排除方法

    有没有办法将代码排除在 Cobertura 覆盖率报告之外 我们有一些方法不应包含在覆盖率报告中 因此不会降低覆盖率数字 我知道Clover有这样的功能 但我还没有找到Cobertura的类似功能 您可以从检测中排除类 那么它们就不应该出现
  • 如何在ListView上长按时传递变量?

    我会有列表视图和里面的很多项目 我希望用户可以长按项目并将其设置为收藏夹 为此 我需要长按此菜单获取数据库 ID 我有以下代码 Override public void onCreateContextMenu ContextMenu men
  • Spring批处理:输入资源不存在类路径资源

    我目前正在开发一个 Spring Batch 它首先将 Excel xsls 文件转换为 CSV 然后读取 CSV 处理它并将其数据存储在数据库中 第一步效果很好 批次在第二步停止并抛出此警告 Input resource does not