spring将缓冲图像转换为响应实体

spring将缓冲图像转换为响应实体,spring,spring-boot,Spring,Spring Boot,我的spring boot应用程序中有BuffereImage。现在我想将该文件发送给用户。我该怎么做 我正在寻找将BuffereImage转换为ResponseEntity的方法。您也可以使用javax.imageio.imageio将其转换为字节[] ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage , "png", byteArrayO

我的spring boot应用程序中有BuffereImage。现在我想将该文件发送给用户。我该怎么做


我正在寻找将BuffereImage转换为ResponseEntity的方法。

您也可以使用
javax.imageio.imageio将其转换为
字节[]

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);
@RequestMapping(value=“/path”,method=GET)
公共响应获取资源(){
返回响应状态(HttpStatus.OK)
.header(HttpHeaders.CONTENT\u处置,“文件名=\”image.png“\”)
.contentType(MediaType.IMAGE\u PNG)
.主体(imageInByte);