Spring Batch:FlatFileItemWriter到InputSteam / Byte数组

时间:2017-11-03 10:38:46

标签: spring-boot spring-batch

所以我创建了一个生成报告(csv文件)的批处理作业。我已经能够使用FlatFileItemWriter无缝地生成文件,但我的最终目标是创建一个InputStream来调用一个rest服务,它将存储文档或一个字节数组以将其存储在数据库中。

public class CustomWriter implements ItemWriter<Report> {

    @Override
    public void write(List<? extends Report> reports) throws Exception {
        reports.forEach(this::writeDataToFile);
    }

    private void writeDataToFile(final Report data) throws Exception {
        FlatFileItemWriter writer = new FlatFileItemWriter();
        writer.setResource(new FileSystemResource("C:/reports/test-report.csv"));
        writer.setLineAggregator(getLineAggregator();
        writer.afterPropertiesSet();
        writer.open(new ExecutionContext());
        writer.write(data);
        writer.close();
    }

    private DelimitedLineAggregator<Report> getLineAggregator(final Report report) {
        DelimitedLineAggregator<Report> delimitedLineAgg = new DelimitedLineAggregator<Report>();
        delimitedLineAgg.setDelimiter(",");
        delimitedLineAgg.setFieldExtractor(getFieldExtractor());
        return delimitedLineAgg;
    }

    private FieldExtractor<Report> getFieldExtractor() {
        BeanWrapperFieldExtractor<Report> fieldExtractor = new BeanWrapperFieldExtractor<Report>();
        fieldExtractor.setNames(COLUMN_HEADERS.toArray(new String[0]));
        return fieldExtractor;
    }

}

我可以这样做的一种方法是临时存储文件并创建一个新步骤来选择生成的文件并进行发送/存储但我真的想跳过这一步并在第一步发送/存储它步。

我该怎么做呢?

0 个答案:

没有答案