0

I am trying to write data into InputStream rather than to a File.

My Writer class extends FlatFileItemWriter

I see FlatFileItemWriter<> has the option to write data to InputStream

I am setting the resource like this

setResource(new InputStreamResource(inputStream));

I am getting the following errors

org.springframework.batch.item.ItemStreamException: Could not convert resource to file: [InputStream resource [resource loaded through InputStream]]
at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:384) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.item.file.FlatFileItemWriter.open(FlatFileItemWriter.java:322) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]

Caused by: java.io.FileNotFoundException: InputStream resource [resource loaded through InputStream] cannot be resolved to absolute file path
at org.springframework.core.io.AbstractResource.getFile(AbstractResource.java:114) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:381) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]

What can be done to rectify this? Or is there any other way to implement this?

The purpose of this is to send the stream to AWS S3 via S3Client which is more comfortable than creating an instance of file and uploading it.

bumi25
  • 125
  • 1
  • 14

1 Answers1

0

Using the FlatFileItemWriter you will not be able to write to an Outputstream directly, it only writes the contents to an output physical file.

You may use the output file to stream data further if needed.

  • Okay but the reason to send the stream directly was to avoid creating intermediate file. So I guess I will have to create an intermediate file. – bumi25 May 12 '18 at 06:23
  • Honestly, sometime back for one of my projects I too was looking for a way to not create a temp file while using FileItemWriter. But if you think about it, Spring batch which is made for handling huge amount of data, the way it works for flat file writer is that after every batch step execution the data in the memory is written to an outside physical flat file at once and not one by one as stream. It opens execution context, writes all data from memory and closes it. This way there is nothing in the memory from the previous step or chunk and it can move on the next step. – Ankit Mahajan May 12 '18 at 07:27
  • Okay. Thanks a lot. – bumi25 May 12 '18 at 07:29
  • 1
    @bumi25, what is the final outcome. I meant how did you handle this scenario, I too was in the same situation. – rk rk Mar 09 '23 at 16:05
  • As far as I can remember, back then we couldn’t write to output stream using FlatFileItemWriter, so I guess I had to provide the file resource name which dumped the data in the file – bumi25 Mar 10 '23 at 17:14