如何使用 HTML 标签向 JSP 页面显示以字节为单位的图像? [英] How to display an image which is in bytes to JSP page using HTML tags?
问题描述
我有 ByteArrayOutputStream,它包含一个以字节为单位的 JPEG 图像.我的要求是在 JSP 页面中显示该图像(使用HTML 标签).我该怎么做?
I have ByteArrayOutputStream which contains a JPEG image in bytes. My requirement is to display that image in a JSP page (to display the image in the frontend using HTML tags). How do I do that?
我已经提到了 BufferedImage
类,但它让我感到困惑,因为我是新手.
I have referred the BufferedImage
class, but it is confusing for me because I am new to this.
推荐答案
如果图片不是太大,可以如下操作.
If the image is not too big, you can do it as follows.
<img src="data:image/jpg;base64,iVBORw0KGgoAAAANS..." />
其中 iVBORw0KGgoAAAANS
... 是 Base64 编码的字节.
Where iVBORw0KGgoAAAANS
... is the Base64 encoded bytes.
Base64 编码可以使用库完成,例如 Ostermiller Java Utilities 的 Base64 Java 库 或 org.apache.commons.codec.binary.Base64.
Base64 encoding can be done with a library, like Ostermiller Java Utilities' Base64 Java Library or org.apache.commons.codec.binary.Base64.
这篇关于如何使用 HTML 标签向 JSP 页面显示以字节为单位的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!