Spring MVC Thymeleaf:将缓冲图像显示为HTML文件

时间:2015-10-20 07:59:00

标签: spring spring-mvc thymeleaf

无法在html文件中显示图片。我哪里出错了?

@RequestMapping(value = "/image/{usr.id}", headers = "Accept=image/jpeg, image/jpg, image/png, image/gif", method = RequestMethod.GET)
public @ResponseBody BufferedImage getImage(@PathVariable("usr.id") Long id) {
    Attachment att = attSvc.getPicById(id);

    try {

        InputStream in = new ByteArrayInputStream(att.getAttachmentFile());
        return ImageIO.read(in);
    } catch (IOException e) {
        System.out.println("ERROR:" + e);
        throw new RuntimeException(e);
    }
}

如果您需要更多参考资料,请告诉我!非常感谢你。

1 个答案:

答案 0 :(得分:0)

这可以解决您的问题。

...
public @ResponseBody byte[] getImage(@PathVariable("usr.id") Long id)  {
   ...  
      InputStream in = new ByteArrayInputStream(att.getAttachmentFile());
      BufferedImage img = ImageIO.read(in); 
      ByteArrayOutputStream bao = new ByteArrayOutputStream();
      ImageIO.write(img, "jpg", bao);
      return bao.toByteArray();
   ...