Hadi
Hadi

Reputation: 17289

How display images that stored in database in spring mvc

I want to display hows detail with images. in fact may be one how not have image. here is my controller to display it

  @RequestMapping(value="/Hows/{categoryId}/{categoryName}",method=RequestMethod.GET)
public String showHowsCategory(@PathVariable("categoryId")int category,Model  model,HttpServletRequest request){

    int page = 1;
    if(request.getParameter("page")!=null)
        page = Integer.parseInt(request.getParameter("page").toString());

     ListHows = showCategoryService.showAllHowInCategory(category,(page- 1)*pageSize,pageSize);
     for(int i=(page-1)*pageSize;i<pageSize;i++){
         byte[] binaryData = ListHows.get(i).getImage();
         if(binaryData != null){
         try {
                byte[] encodeBase64 = Base64.encode(binaryData);
                String base64Encoded = new String(encodeBase64, "UTF-8");
                imageList.add(base64Encoded);

            } catch (Exception e) {
                e.printStackTrace();
            }
         }
     }
     model.addAttribute("image", imageList);
     model.addAttribute("howList",ListHows); 

and in jsp view use this code. but not display images.

  <c:forEach var="how" items="${howList}" varStatus="counter">
             <c:set var="url" value="${how.how}"/>
              <c:set var="count" value="0"  />
             <div class="table">
            <div style="float: left;margin-left: 10px;">
             <c:if test="${how.image != null}">
                         <img  src="data:image/jpeg;base64,${image[count]}"  width="150" height="120"/>
                           <c:set var="count" value="${count + 1}" />
                      </c:if>
                      <c:if test="${how.image == null}">
                        <img width="150" height="120" src="<c:url value="/resources/images/how.jpg" />"/>
                      </c:if>
                      </div>

what is my problem?

Upvotes: 1

Views: 1573

Answers (1)

Gurkan İlleez
Gurkan İlleez

Reputation: 1573

 <img  src="data:image/jpeg;charset=utf-8;base64,${image[count]}"  width="150" height="120"/>

data will be the encoded base64 string.

Upvotes: 1

Related Questions