Java 从字节数组设置 img src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30129486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:12:17  来源:igfitidea点击:

Set img src from Byte Array

javajavascripthtmlimagejsp

提问by Rodrigo Juarez

I need to set the img srcproperty from a byte array that I have in a Object.

我需要img src从对象中的字节数组设置属性。

<img id="profileImage">
    <spring:bind path="object.profilePicture">
        <input type="file" name="profilePicture" id="profilePicture" path="profilePicture">
    </spring:bind>

I need to display that byte array in the imgabove the input tag.

我需要在img上面的输入标签中显示该字节数组。

回答by DonO

Replace the jpgwith the type of image, and [your byte array]with your byte array. You need to convert it to base64if it isn't already.

将 替换jpg为图像类型,并 [your byte array]替换为您的byte array. base64如果还没有,您需要将其转换为。

<img id="profileImage" src="data:image/jpg;base64, [your byte array]">

回答by Francisco M

In my case the base64 byte array had to be chunked, in the way:

在我的情况下,base64 字节数组必须分块,方式如下:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QCARXhpZgAATU0AKgAAAAgABAEaAAUAAAABAAAAPgEbAAUA
AAABAAAARgEoAAMAAAABAAIAAIdpAAQAAAABAAAATgAAAAAAAABIAAAAAQAAAEgAAAABAAOQAAAH
...

To make this solution works.

为了使这个解决方案有效。

回答by Serj Sagan

I use this site to convert images to the base64byte array data, it gives me the tags in htmland cssso I just upload and copy/paste.

我使用这个网站将图像转换为base64字节数组数据,它给了我标签htmlcss所以我只需上传和复制/粘贴。

http://base64.wutils.com/encoding-online/

http://base64.wutils.com/encoding-online/