我想实现一个Liferay Portlet,它从一个单独的服务器下载~1GB文件,并将其提供给点击该链接的网站访问者。
该文件必须以内存高效的方式进行流式处理(因此不要将所有内容加载到内存中),并且用户应在单击后立即看到下载进度(因此不会将所有内容存储到本地磁盘上)。
我必须使用WebClient,因为它似乎是在Liferay 7中制作Web请求的标准(RestTemplate将是deprecated)。
我开始写这样的东西,灵感来自javadoc的一个例子:
Mono<DataBuffer> bodyMono = client.get()
.uri("https://theotherserver.com/file94875.pdf")
.retrieve()
.bodyToMono(DataBuffer.class);
...我将通过MVCResourceCommand.serveResource()
进入portlet的PortletResponseUtil.sendFile
,它需要一个java.io.InputStream
。
不幸的是,WebClient给了我一个Mono<DataBuffer>
(或Flux<DataBuffer>
),另一个答案声称reconstructing the InputStream defeats the purpose of using WebClient in the first place。
什么是最有效和WebClient精明的方式来实现这一点?
在Liferay的情况下,文档声明,您可以使用.... getPortletOutputStream()来检索OutputStream。设置contentlengh后(浏览器知道要多少钱),您可以使用:Convert writes to OutputStream into a Flux<DataBuffer> usable by ServerResponse
将数据写入OutputStream