I am uploading streams of (raw bytes) data using HTTP posts using WebClient:
final byte[] rawData = IOUtils.toByteArray(sourceInputStream);
webClient.post()
.uri(uri)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.bodyValue(rawData)
.exchange()...
I am concerned there is a potentially a lot of memory used given sometimes these objects can be quite big (~200Mb) so would like to read directly from the InputStream and upload as a stream.
I tried:
bodyValue(BodyInserters.fromResource(new InputStreamResource(inputStream)))
but got exception Content type 'application/octet-stream' not supported for bodyType=org.springframework.web.reactive.function.BodyInserters
So I then tried removing the header but the data is then corrupted.
Is there a way to stream the data without passing through the in memory 'buffer' rawData[]?
Thanks
Your first try was almost correct, however you need to use body(...)
instead of bodyValue(...)
:
body(BodyInserters.fromResource(new InputStreamResource(inputStream)))
This is because bodyValue(...)
wraps your resource inserter in a value inserter, which will then try to serialize the resource inserter itself and fail with the error you received.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With