![](/img/trans.png)
[英]How do you use Spring WebClient as a file download passthru
[英]Spring WebClient to Download an Image
我一直在研究响应式编程,最近尝试使用Spring WebFlux构建POC。 我想从简单开始,仅使用WebClient下载图像; 特别是https://greatatmosphere.files.wordpress.com/2013/02/great-atmosphere-149-tenaya-lake-yosemite-national-park-2.jpg
我尝试了以下代码
String block = WebClient.create("https://greatatmosphere.files.wordpress.com/2013/02/great-atmosphere-149-tenaya-lake-yosemite-national-park-2.jpg")
.get()
.accept(MediaType.IMAGE_JPEG)
.retrieve()
.bodyToMono(String.class)
.doOnError(WebClientResponseException.class,
ex -> System.out.println(ex.getStatusCode() + ": " + ex.getResponseBodyAsString()))
.log()
.block();
System.out.println("output:" + block);
但它不能按预期工作。 似乎数据一直在被流式传输,并且get请求不会终止。
我敢肯定,我缺少一些简单的东西,但似乎无法弄清楚。 我尝试了各种参数,但结果是相同的。
如何使用WebClient下载图像然后终止?
将图像捕获为byte[]
而不是String
byte[] image = WebClient.create("https://greatatmosphere.files.wordpress.com/2013/02/great-atmosphere-149-tenaya-lake-yosemite-national-park-2.jpg")
.get()
.accept(MediaType.IMAGE_JPEG)
.retrieve()
.bodyToMono(byte[].class)
.block();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.