在 Spring WebClient 中流式上传“POST" [英] Stream upload 'POST' in Spring WebClient

查看:34
本文介绍了在 Spring WebClient 中流式上传“POST"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebClient 使用 HTTP 帖子上传(原始字节)数据流:

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()...

我担心可能会使用大量内存,因为有时这些对象可能非常大(~200Mb),因此想直接从 InputStream 读取并作为流上传.

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.

我试过了:

 bodyValue(BodyInserters.fromResource(new InputStreamResource(inputStream))) 

但得到异常内容类型application/octet-stream"不支持 bodyType=org.springframework.web.reactive.function.BodyInserters

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.

有没有办法在不通过内存缓冲区"rawData[]的情况下流式传输数据?

Is there a way to stream the data without passing through the in memory 'buffer' rawData[]?

谢谢

推荐答案

我最终保留了 rawData[] 缓冲区并在请求中指定了 contentLength:

I ended up keeping the rawData[] buffer and specifying contentLength in the request:

webClient.post()
         .uri(uri)
         .contentType(MediaType.APPLICATION_OCTET_STREAM)
         .contentLength(bytes.length)
         .bodyValue(bytes)
         .exchange()
         .block(Duration.ofSeconds(30));

对于非常大的文件,我进行了分块上传 - 例如 1Mb 的文件块可能如下所示:

For very large files I did chunked uploads - for example 1Mb chunks of a file might look like:

POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/StartUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678')?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'
POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/ContinueUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678',fileOffset=1048576)?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'
POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/ContinueUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678',fileOffset=2097152)?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'
POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/ContinueUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678',fileOffset=3145728)?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'
POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/ContinueUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678',fileOffset=4194304)?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'
POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/ContinueUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678',fileOffset=5242880)?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'
POST: https://sharepoint.acme.com:443/sharepoint-site/_api/web/GetFileByServerRelativeUrl(@f)/FinishUpload(uploadId=guid'680f93eb-8c6a-443d-87ad-1e1b324ea678',fileOffset=6291456)?@f='/sharepoint-site/folderA/subFolderB/etc/example.file'

由处理原始字节驱动:

final UUID id = UUID.randomUUID();
int offset = 0;
while (offset < bytesTotal) {
    final URI uri;
    if (offset == 0) {
        uri = createStartUri(id, path, filename);
    } else if (offset < bytesTotal - CHUNK_SIZE) {
        uri = createContinueUri(id, offset, path, filename);
    } else {
        uri = createFinishUri(id, offset, path, filename);
    }
    final byte[] bytes = ArrayUtils.subarray(fileDataBytes, offset, offset + CHUNK_SIZE);

    webClient.post().uri(uri).contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(bytes.length)
            .bodyValue(bytes).exchange().block(Duration.ofSeconds(30));
    
    offset += CHUNK_SIZE;
}

这篇关于在 Spring WebClient 中流式上传“POST"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆