【问题标题】:spring convert buffered image into response entity弹簧将缓冲图像转换为响应实体
【发布时间】:2020-01-17 11:45:06
【问题描述】:

我的 Spring Boot 应用程序中有 BufferedImage。现在我想将该文件发送给用户。我该怎么做?

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

【问题讨论】:

标签: spring spring-boot


【解决方案1】:

您也可以使用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);

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 2019-01-10
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多