return png 文件是否有变化,例如 response.png 而不是 response.bin
Is there a change to return a png file like response.png instead of response.bin
我的代码的目的是从第三方服务检索图像。
我努力让下载端点正常工作,但只成功了一部分。当我通过邮递员调用端点时,答案是一个 .bin 文件,但我需要的是一个 .png 文件。最大的成功是能够获得一个能够自定义名称的 .png 文件。但个性化并不是绝对必要的。
该项目是使用初始化程序构建的,具有以下依赖项:
- spring-boot-starter-web;
- 龙目岛
- spring-boot-starter-webflux
- 反应堆-spring
下面是我端点的源代码:
@GetMapping("/retrieve-image")
public Mono<byte[]> retrieveImage(ImageRequest request) throws ExecutionException, InterruptedException, IOException {
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
queryParams.add("attribute", request.getAttribute()); // fake for query string setting.
Mono<byte[]> image = webClient
.get()
.uri(uriBuilder -> uriBuilder
.path(Endpoint.THIRD_PARTY_SERVICE_URI)
.queryParams(queryParams)
.build())
.accept(MediaType.valueOf(String.valueOf(MediaType.IMAGE_PNG)))
.exchange()
.flatMap(clientResponse -> clientResponse.bodyToMono(byte[].class)
.doOnSuccess(body -> {
if (clientResponse.statusCode().isError()) {
log.error("HttpStatusCode = {}", clientResponse.statusCode());
log.error("HttpHeaders = {}", clientResponse.headers().asHttpHeaders());
log.error("ResponseBody = {}", body);
}
}));
return image;
}
您还可以将文件的 mime 类型添加到 @GetMapping
注释的 produces 部分,它应该看起来像这样:
@GetMapping(path = "/retrieve-image",
produces = "image/png")
此外,您可以将响应包装在 ResponseEntity<Resource>
中,而不是返回 Mono<byte[]>
。这使您可以添加 Headers 并告诉浏览器您的文件名。例如:
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=image.png");
header.add("Access-Control-Expose-Headers", "Content-Disposition");
return ResponseEntity.ok().
.headers(header)
.contentLength(Files.size(path))
.body(<<YOUR_FILE_HERE>>);
最后一个想法:如果您将 spring-boot-starter-web 和 spring-boot-starter-webflux 添加到您的依赖项中,该应用程序将运行,但它不使用 Webflux 中的 Netty,而是通常的 Tomcat.因此,您不会从响应式功能中受益。
我的代码的目的是从第三方服务检索图像。
我努力让下载端点正常工作,但只成功了一部分。当我通过邮递员调用端点时,答案是一个 .bin 文件,但我需要的是一个 .png 文件。最大的成功是能够获得一个能够自定义名称的 .png 文件。但个性化并不是绝对必要的。
该项目是使用初始化程序构建的,具有以下依赖项:
- spring-boot-starter-web;
- 龙目岛
- spring-boot-starter-webflux
- 反应堆-spring
下面是我端点的源代码:
@GetMapping("/retrieve-image")
public Mono<byte[]> retrieveImage(ImageRequest request) throws ExecutionException, InterruptedException, IOException {
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
queryParams.add("attribute", request.getAttribute()); // fake for query string setting.
Mono<byte[]> image = webClient
.get()
.uri(uriBuilder -> uriBuilder
.path(Endpoint.THIRD_PARTY_SERVICE_URI)
.queryParams(queryParams)
.build())
.accept(MediaType.valueOf(String.valueOf(MediaType.IMAGE_PNG)))
.exchange()
.flatMap(clientResponse -> clientResponse.bodyToMono(byte[].class)
.doOnSuccess(body -> {
if (clientResponse.statusCode().isError()) {
log.error("HttpStatusCode = {}", clientResponse.statusCode());
log.error("HttpHeaders = {}", clientResponse.headers().asHttpHeaders());
log.error("ResponseBody = {}", body);
}
}));
return image;
}
您还可以将文件的 mime 类型添加到 @GetMapping
注释的 produces 部分,它应该看起来像这样:
@GetMapping(path = "/retrieve-image",
produces = "image/png")
此外,您可以将响应包装在 ResponseEntity<Resource>
中,而不是返回 Mono<byte[]>
。这使您可以添加 Headers 并告诉浏览器您的文件名。例如:
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=image.png");
header.add("Access-Control-Expose-Headers", "Content-Disposition");
return ResponseEntity.ok().
.headers(header)
.contentLength(Files.size(path))
.body(<<YOUR_FILE_HERE>>);
最后一个想法:如果您将 spring-boot-starter-web 和 spring-boot-starter-webflux 添加到您的依赖项中,该应用程序将运行,但它不使用 Webflux 中的 Netty,而是通常的 Tomcat.因此,您不会从响应式功能中受益。