如何使用Java在HTML中显示字节[]?
我在项目中使用Spring MVC和JPA。我将文件作为byte []
获取,然后保存在数据库中。但是,当我想在< img>
html标签中显示时,它不会显示出来。
我的实体是:
class Photo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
@Lob
private byte[] profilePic;
// getter and setter
}
数据库中的值是:
{
"id": 4,
"title": "pic 1",
"profilePic": "ZGF0YTppb...VFtQ0M=",
}
这就是我尝试在html中显示图像的方式:
<img src='ZGF0YTppb...VFtQ0M=' />
//or
<img src='data:image/jpeg;base64,ZGF0YTppb...VFtQ0M=' />
该如何显示照片?
谢谢。
I use Spring MVC and JPA in my project. I get file as byte[]
and save in Database. But when I want to display in <img>
tag of HTML it doesn't get displayed.
My entity is:
class Photo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
@Lob
private byte[] profilePic;
// getter and setter
}
Value in the Database is:
{
"id": 4,
"title": "pic 1",
"profilePic": "ZGF0YTppb...VFtQ0M=",
}
And this is how I try to display the image in HTML:
<img src='ZGF0YTppb...VFtQ0M=' />
//or
<img src='data:image/jpeg;base64,ZGF0YTppb...VFtQ0M=' />
What to do to display the photo?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设它的base64编码:
&lt; img src ='data:image/jpeg; base64,zgf0ytppb ... vftq0m ='/&gt;
基本上您可以根据此格式使用数据URL,取决于此格式[您要显示:
数据:[&lt; mime type&gt;] [; charset =&lt; charset&gt;] [; base64],&lt;编码数据&gt;
Assuming it's base64 encoded:
<img src='data:image/jpeg;base64,ZGF0YTppb...VFtQ0M=' />
Basically you can use data urls with this format depending on what content [type] you want to display:
data:[<mime type>][;charset=<charset>][;base64],<encoded data>
HTML:
JS:
假设您的图像以PNG格式(和Base64编码)存储。对于其他图像格式(JPEG等),您需要在URL中更改MIME类型(“ Image/...”部分)。
HTML:
JS:
This assumes that your image is stored in PNG format (and base64 encoded). For other image format (JPEG, etc.), you need to change the MIME type ("image/..." part) in the URL.