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.


data:URI, so don’t try to encode it again.data:image/png;base64,iVBORw…becamedata:image/jpeg;base64,ZGF0YTp….profilePic(and associated getter and setter) toString? Does the framework pass the data as-is then, rather than base64 encoded?profilePicasString(because what you showed as database contents,data:image/png;base64,iVBORw…is a string). The reason you see[100, 97, 116, 97, 58, 105…when inspecting the data in the Java runtime is because that’s what you can expect when inspecting a variable of typebyte[]. The framework used to export the byte array as JSON apparently decided to use base64 encoding forbyte[]. It surely wouldn’t do that if the property had a string type. Using a string type could solve all problems…'ZGF0YTppb...VFtQ0M='string on the Javascript side, likeatob('ZGF0YTppb...VFtQ0M=')you also get the correctdata:URI.