简体   繁体   中英

JSP display image from byte array in Java object

I have complex Java Object List contains a byte array as image format retrieved from DB. There are file name, file type and file data in byte array format etc. in the POJO. byte array is about 20k in length. I can display it in JSP by javascript as:

for(var i in data){
   var imgelement = document.createElement("IMG");
   imgelement.id=data[i].fileId;
   imgelement.src = "data:"+data[i].fileType+";base64,"+data[i].thumbnail;
}

This will display IMG properly.

But when I try to put it in my plain JSP by using jstl foreach function to iterate my POJO list, I never get my image display.

<c:forEach items="${searchResult}" var="current" varStatus="i">
    <c:choose>
        <c:when test="${(i.count) % 2 == 0}">
            <c:set var="rowclass" value="rowtwo" />
        </c:when>
        <c:otherwise>
            <c:set var="rowclass" value="rowone" />
        </c:otherwise>
    </c:choose>
    <tr class="${rowclass}">

        <td nowrap="nowrap" class="tabletd"> <img src="data:${current.miniFile.fileTyle};base64,<c:out value='${current.miniFile.thumbnail}'/>"/> &nbsp;</td>
        <td nowrap="nowrap" class="tabletd"> ${current.site.siteName} &nbsp;</td>
        <td nowrap="nowrap" class="tabletd"> ${current.distance} &nbsp;</td>
        <td nowrap="nowrap" class="tabletd"> ${current.site.siteAccessby} &nbsp;</td>
        <td nowrap="nowrap" class="tabletd"> ${current.site.siteAddressFormated} &nbsp;</td>
    </tr>
</c:forEach>

There is the out put in my html page shows, the src as a byte array:

<img src="data:image/jpeg;base64,[B@2e29c573">

Any idea? Advice please!

Edit

I am trying to use Java 8 Java.util.Base64's function by jsp:useBean to do the byte array convert, but it is still not works for me.

Other question, How I can use jsp:useBean to do it? as the variable is jstl variable. ie I need to use jsp:usedBean function to handle jstl local variable.

code like:

<tr class="${rowclass}">
    <jsp:useBean id="obj" class="java.util.Base64"/> 
    <jsp:setProperty name="imageStr" property="String" value="${current.miniFile.thumbnail}"/>
    <td nowrap="nowrap" class="tabletd"> <img src="data:${current.miniFile.fileTyle};base64,<c:out value='${current.miniFile.thumbnail}'/>"/> &nbsp;</td>

I don't have experience to handle jstl variable by using jsp:useBean, who have such experience, please share to me! Appreciated!

EDIT AGAIN

There is an example to do the Date type charge by using jsp:useBean

[B@2e29c573 means you are simply printing out a byte array. You have to convert the raw byte array into a base64 encoded string. You can use something like Base64.encodeBase64String from commons-codec to do it

Somehow, I never get it works in my JSP. Seems there are some difficult to handle variables in jstl forEach iterator. When I tried to handle it by calling java method, JSP parser always throw parse exception.

Eventually, I have to change my server side code to update my POJO to support Base64 encode string instead of byte array.

I used Java 8 Base64 to do it. It works fine.

Hope this will help some one else in my situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM