Spring Batch Item Reader传递文件对象而不是资源名称?

时间:2014-11-12 08:57:39

标签: spring-batch spring-integration

通常,ItemReader将资源名称作为属性,我们可以将文件对象传递给任何ItemReader的实现。

我正在使用3版本的Spring Batch API。

更新:::

<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">

    <!-- Read a csv file -->
    <property name="resource" value="classpath:cvs/I_10000_3ColRem_input_File.csv" />

    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

            <!-- split it -->
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="customerId,year,month,numPurchases,sow,purchaseAmt,cm,mc,multiChannel,loyalty,productReturn,relationDur,cb" />
                </bean>
            </property>

            <property name="fieldSetMapper">

                <!-- return back to reader, rather than a mapped object. -->

                <!-- map to an object -->
                <bean
                    class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="testCLV" />
                    <property name="customEditors">
                      <map>
                        <entry key="java.lang.Double">
                             <ref local="doubleEditor" />
                        </entry>
                  </map>
                </property>
                </bean>
            </property>
        </bean>
    </property>

</bean>

我的App.java看起来像

ApplicationContext context = 
            new ClassPathXmlApplicationContext(springConfig);

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("reportJob");



    try {
        long a, b;
        a = System.currentTimeMillis();
        JobExecution execution = jobLauncher.run(job, new JobParameters());

        b = System.currentTimeMillis();
        System.out.println("Exit Status : " + execution.getStatus());
        System.out.println("jobLauncher.run "+(b-a)+"mil to execute. ("+((b-a)/1000)+" seconds)");
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Done");

我的要求是遵循::

通过网络应用程序用户将上传一个文件,我从中提取inputStream

假设我有一个streamInput obj作为&#39; streamInput&#39;,我怎么能将它注入ItemReader的资源并运行我的工作。

1 个答案:

答案 0 :(得分:0)

您可以从ResourceInputStream创建一个InputStreamResource,您可以从File对象获取该InputStreamResource。您可以在此处详细了解{{1}}:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/InputStreamResource.html