Java – Use hibernate to retrieve blobs from a database and display them on a web page using jsp

Use hibernate to retrieve blobs from a database and display them on a web page using jsp… here is a solution to the problem.

Use hibernate to retrieve blobs from a database and display them on a web page using jsp

Answer mentioned here, this doesn’t work for me
I’ve sent a byte array to a database that is stored as a blob in the database

File file = new File(filePath);
byte[] imageData = new byte[(int) file.length()];

When I try to retrieve a blob object from the database in that byte array

I get a value like “[B@526d24d9”.
I send this value to the jsp page. (I send the blob list to the jsp page, which is a list of byte arrays).
Now I’m trying to render this image on a web page using jsp. But I can’t figure out the most effective way
One way is to retrieve the blob list, process it and store it in a file, and then use tags to retrieve it from the file path in the JSP page
But I’m looking for a more efficient way.
I am trying to something like this

JSP code

<c:forEach items="${list}" var="list" varStatus="loop">
   <c:set var="l" value="${loop.index}" />

<tr>
    <td><c:out value= "${l+1}" /></td>
      <td><c:out value="${list.name}" /></td>
      <td><c:out value="${list.size} MB" /></td>
      <td><c:out value="${list.preview}" /></td>
      <td><i class="material-icons">edit</i>
      <i class="material-icons" onclick="Remove()">delete</i></td>
    </tr>
  </c:forEach>

list.preview contains the byte array “[B@526d24d9”

Solution

Create a string previewUrl field in your entity class.
And write this code in the getter.

public String getPreviewUrl() {
        String pu = Base64.encode(getPreview());
        setPreviewUrl(pu);
        return previewUrl;
    }

In your jsp code,

<td><img class='imagem_artigo' src='data:image/png; base64,${list.previewUrl}' alt='IMG DESC' width="200" height='200'></td>

This will work

Related Problems and Solutions