Stream upload 'POST' in Spring WebClient

亡梦爱人 提交于 2021-01-04 03:24:31

问题


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

来源:https://stackoverflow.com/questions/61506325/stream-upload-post-in-spring-webclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!