【发布时间】:2020-01-17 11:45:06
【问题描述】:
我的 Spring Boot 应用程序中有 BufferedImage。现在我想将该文件发送给用户。我该怎么做?
我正在寻找将 BufferedImage 转换为 ResponseEntity 的方法。
【问题讨论】:
标签: spring spring-boot
我的 Spring Boot 应用程序中有 BufferedImage。现在我想将该文件发送给用户。我该怎么做?
我正在寻找将 BufferedImage 转换为 ResponseEntity 的方法。
【问题讨论】:
标签: spring spring-boot
您也可以使用javax.imageio.ImageIO 将其转换为byte[]
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage , "png", byteArrayOutputStream);
byte[] imageInByte = baos.toByteArray();
那么你的控制器就被简化了:
@RequestMapping(value = "/path", method = GET)
public ResponseEntity<byte[]> getResource() {
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.CONTENT_DISPOSITION, "filename=\"image.png"\")
.contentType(MediaType.IMAGE_PNG)
.body(imageInByte);
【讨论】: