base64转换字节数组到一个图像 [英] Convert base64 byte array to an image

查看:168
本文介绍了base64转换字节数组到一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个form bean的属性与ID,递减和imageByteArray。 Struts动作会被执行并且我想要访问这些bean的属性一样的id,递减和转换时,imageByteArray并显示为图像重定向到一个JSP。我想这<一个href=\"http://stackoverflow.com/questions/943729/how-to-display-an-image-which-is-in-bytes-to-jsp-page-using-html-tags\">post,但是,这不是为我工作。

I have a form bean with attributes id, desc and imageByteArray. Struts action gets executed and it redirects to a JSP where i want to access these bean attributes like id, desc and convert the imageByteArray and display it as an image. I tried this post, but that's not working for me.

我带code。使用Base64编码的ByteArray - 在this.bean.imageByteArray指的form bean

I encode the bytearray using Base64 - where this.bean.imageByteArray refers to the form bean

this.bean.setImageByteArray(new org.apache.commons.codec.binary.Base64().encode(imageInByteArr));

我想这一点,但不工作

I tried this, but not working

<img src="data:image/jpg;base64,<c:out value='${bean.imageByteArray}'/>" />

字节数组(字节[] imageByteArray)是指一个base64连接codeD JPG图片,我发现了以下img标签作为输出,显然没有得到显示,

Byte array (byte[] imageByteArray) refers a base64 encoded JPG image and I'm getting the following img tag as output and obviously nothing gets displayed,

<img src="data:image/jpg;base64,[B@2e200e">

不知道如何为base64字节数组,并显示转换为JSP的形象?

Any idea how to convert base64 byte array and display as an image in JSP?

推荐答案

你得到的仅仅是一个数组的toString输出。但是,您需要将字节数组转换为字符串。

What you get is just the toString output of an array. You need however the byte array converted to a String.

您应该创造豆的方法


public String getByteArrayString()
{
   return new String(this.imageByteArray);
}

和在你的JSP参考这一点。

and reference this in your JSP.

虽然在技术上应该定义用于进行Base64字节数组,它编码的所有字符都在标准的7位ASCII范围,这是没有必要的。

While technically you should define which encoding to use for an array of base64 bytes this is not necessary as all characters are in the standard 7bit ASCII range.

这篇关于base64转换字节数组到一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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