简体   繁体   中英

Get file name from readMultiFileJob in Spring Batch

The following is my Spring Batch processing config file, i am reading multiple files (xml, csv etc), the files generate dynamically with time stamp as suffix i can read file's data and process,
now the Question is, i would like know the file name. How to get file name when job is processing.

<import resource="../config/context.xml" />
    <bean id="domain" class="com.di.pos.Domain" />
    <job id="readMultiFileJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1">
            <tasklet>
                <chunk reader="multiResourceReader" writer="flatFileItemWriter"
                    commit-interval="1" />
            </tasklet>
        </step>
    </job>
    <bean id="multiResourceReader"
        class=" org.springframework.batch.item.file.MultiResourceItemReader">
        <property name="resources" value="file:csv/inputs/dipos-*.csv" />
        <property name="delegate" ref="flatFileItemReader" />
    </bean>
    <bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <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="names" value="id, name" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="domain" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
  1. Create a custom Mapper which extends LineMapper.
  2. Override mapLine() method

public FileData mapLine(String line, int lineNumber) throws Exception { FileData fileData = new FileData();

        Resource currentResource = delegator.getCurrentResource();
        String[] fileName = currentResource.getFilename().split("/");

        //Use this to access file path
        URI fileUri = currentResource.getURI();
        return fileData;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM