简体   繁体   English

如何在Spring MVC中显示存储在数据库中的图像

[英]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. 并在jsp视图中使用此代码。 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字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Angular 4 在 Spring Boot 中显示存储的图像? - How to display stored images in Spring Boot using Angular 4? 如何在 Spring MVC 中显示多个 base64 编码的图像 - How to display multiple base64 encoded images in Spring MVC MVC数据库图像显示弹簧 - MVC Database Image Display spring Spring MVC如何将数据从数据库显示到表中 - Spring MVC how to display data from database into a table 无法使用Spring MVC在JSP中显示上传的图像(保存在文件系统中以及数据库中的存储位置,例如C:\\ Database \\ ..) - Unable to display uploaded image (saved in file system and stored location on database eg., C:\Database\.. ) in jsp using spring mvc 如何使用存储在子表中的路径显示图像 - Angular 和 Spring boot - how can I display images using a path stored in a child table- Angular and Spring boot 如何将已经存储在MySQL数据库中的图像转换为本地存储的图像 - How to convert images already stored in MySQL database to images stored locally Spring MVC显示数据库中的数据 - Spring MVC display data from database MVC Spring Java - 在类中显示存储为字段的图片 - MVC Spring Java - display picture stored as field in the class 如何在我的JSP(基于注释的Spring Web MVC项目)中显示图像? - How do I display images in my JSP (Annotation based Spring Web MVC Project)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM