将 base64 字节数组转换为图像 [英] Convert base64 byte array to an image

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

问题描述

我有一个具有 id、desc 和 imageByteArray 属性的表单 bean.Struts 动作被执行,它重定向到一个 JSP,我想在那里访问这些 bean 属性,如 id、desc 并转换 imageByteArray 并将其显示为图像.我试过这个 发布,但这对我不起作用.

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.

我使用 Base64 对字节数组进行编码 - 其中 this.bean.imageByteArray 指的是表单 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));

我试过了,但没有用

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

字节数组 (byte[] imageByteArray) 指的是 base64 编码的 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">

知道如何在 JSP 中转换 base64 字节数组并显示为图像吗?

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.

你应该在 bean 中创建一个方法

You should create a method in bean


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天全站免登陆