gpt4 book ai didi

spring - Spring WebClient 流上传 'POST'

转载 作者:行者123 更新时间:2023-12-04 11:33:47 27 4
gpt4 key购买 nike

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

    final byte[] rawData = IOUtils.toByteArray(sourceInputStream);
webClient.post()
.uri(uri)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.bodyValue(rawData)
.exchange()...

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

我试过:
 bodyValue(BodyInserters.fromResource(new InputStreamResource(inputStream))) 

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

所以我然后尝试删除标题,但数据随后被损坏。

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

谢谢

最佳答案

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

webClient.post()
.uri(uri)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(bytes.length)
.bodyValue(bytes)
.exchange()
.block(Duration.ofSeconds(30));
对于非常大的文件,我进行了分块上传 - 例如 1Mb 的文件块可能如下所示:
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 - Spring WebClient 流上传 'POST',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61506325/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com