简体   繁体   中英

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?

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

数据将是编码的base64字符串。

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