转换的byte []为Base64字符串数据URI

问题描述:

我知道这可能已经问10000次,但是,我似乎无法找到一个直接的答案的问题。

I know this has probably been asked 10000 times, however, I can't seem to find a straight answer to the question.

我保存在我的数据库是重新presents图像的LOB;我正在从数据库中的形象,我想表明它通过HTML IMG标记在网页上。这不是我的preferred的解决方案,但它是一个权宜之计,实施直到我能找到更好的解决办法。

I have a LOB stored in my db that represents an image; I am getting that image from the DB and I would like to show it on a web page via the HTML IMG tag. This isn't my preferred solution, but it's a stop-gap implementation until I can find a better solution.

我想使用Apache共享codeC以下列方式为字节[]转换为Base64:

I'm trying to convert the byte[] to Base64 using the Apache Commons Codec in the following way:

String base64String = Base64.encodeBase64String({my byte[]});

然后,我想表达我的网页这样我的图片:

Then, I am trying to show my image on my page like this:

<img src="data:image/jpg;base64,{base64String from above}"/>

它的显示浏览器的默认我找不到这个形象,形象。

It's displaying the browser's default "I cannot find this image", image.

有没有人有什么想法?

感谢。

我用这个和它工作得很好(违背了公认的答案,它使用不推荐使用此方案的格式):

I used this and it worked fine (contrary to the accepted answer, which uses a format not recommended for this scenario):

StringBuilder sb = new StringBuilder();
sb.append("data:image/png;base64,");
sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(imageByteArray, false)));
contourChart = sb.toString();