将 base64 字节数组转换为图像

编程入门 行业动态 更新时间:2024-10-27 03:41:38
本文介绍了将 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.apachemons.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 字节数组转换为图像

本文发布于:2023-06-04 08:38:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/493389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   字节   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!