Writing data into a InputStream with FlatFileItemWriter

1.5k Views Asked by At

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.

1

There are 1 best solutions below

5
On

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.