繁体   English   中英

使用Spring在JSP中显示图像

[英]display image in JSP using Spring

我正在尝试在注入的jsp中显示图像。 我上载图像并将其存储在数据库中,但是不知道如何在jsp中检索和显示图像。 我的控制器:

@RequestMapping(value = "/ver2", method = RequestMethod.GET)
public void ver2(HttpSession session, HttpServletResponse response) {
    OutputStream oImage;
    Item item10 = itemRepository.findOne(11);

    try {
        byte[] photo = item10.getImagen();
        response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
        oImage = response.getOutputStream();
        oImage.write(photo);
        oImage.flush();
        oImage.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

使用此代码,我将显示全屏,并且需要在jsp中注入。 任何想法?

谢谢

您需要将String中的Base64编码的图像字节返回到JSP页面并使用:

<img src="data:image/png;base64,${yourBase64EncodedBytesString}"/>

显示您的图像。

使用Apache Commons Codec进行Base64编码。

因此例如:

String yourBase64EncodedBytesString = new String(Base64.encodeBase64(content));

将其设置为例如请求属性:

request.setAttribute("yourBase64EncodedBytesString", yourBase64EncodedBytesString);

并在JSP页面中检索:

<img src="data:image/png;base64,${requestScope['yourBase64EncodedBytesString']}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM