String imageUrl;
// 使用WebClient下载图片
WebClient webClient = WebClientUtil.getWebClient();
Mono<ByteArrayOutputStream> byteArrayOutputStreamMono = webClient.get()
.uri(imageUrl)
.retrieve()
.bodyToFlux(DataBuffer.class) // 获取图片内容的DataBuffer流
.reduce(new ByteArrayOutputStream(), (baos, dataBuffer) -> {
try {
Channels.newChannel(baos).write(dataBuffer.asByteBuffer().asReadOnlyBuffer());
} catch (IOException e) {
throw new RuntimeException("Error writing to ByteArrayOutputStream", e);
}
return baos;
});
使用这段代码时,如果imageUrl中包含%,则它会被自动转为%25,导致404 Not Found错误,怎么办?
一开始用URLEncoder.encode(imageUrl, StandardCharsets.UTF_8);
之类的方法,达不到预期
URI uriObj = URI.create(url);即可
https://blog.youkuaiyun.com/qq_38595432/article/details/129706788