转换的byte []为Base64字符串数据URI [英] Convert byte[] to Base64 string for data URI

查看:149
本文介绍了转换的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();

这篇关于转换的byte []为Base64字符串数据URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆