您的位置:首页 > 数据库

将图片以二进制格式存入数据库,并以流的方式展现在jsp页面

2015-05-04 11:37 405 查看


1.     简介

项目新需求,管理员有权限创建应用,应用字段包括:id、softname、softunique、softimage;上传图片时,数据库存储二进制,当普通用户首页展示图片时需要以流的方式展现,全程无文件存储目录,图片不保存在服务器。本demo以springmvc框架搭建,上传文件使用的是:

@RequestParam MultipartFile myfile

2.     数据库表字段

本demo使用mysql数据库,存储二进制的字段名为:blob

create table softwareinfo(
   sid varchar(60)
primary key,
   softname varchar(20),
   softurl varchar(100),
   softcommand varchar(60),
   softimagepath varchar(100),
   softimage blob,
   softremark varchar(100),
   softindex integer)

3.     上传逻辑

 

@RequestMapping(value =
"/soft/create", method = { RequestMethod.POST,
           RequestMethod.GET })
    public ModelAndView create(ModelMap
model,
           @RequestParam MultipartFile myfile, SoftEntity se,
           HttpServletRequest request, HttpServletResponse response)
           throws Exception {
 
       if (se ==
null) {
           throw
new
Exception("应用创建异常");
       }
 
       log.info("日志:应用创建");
 
       se.setSid(UUID.randomUUID().toString());
 
       /*
        * System.out.println("文件长度: " + myfile.getSize());
        * System.out.println("文件类型: " + myfile.getContentType());
        * System.out.println("文件名称: " + myfile.getName());
        * System.out.println("文件原名: " + myfile.getOriginalFilename());
        * System.out.println("other: " + myfile.getBytes());
        */
//注意此处:一下注释的方法在jdk1.5时会报错,仅支持1.6以上
//     InputStream is = myfile.getInputStream();
//     BufferedImage bi = ImageIO.read(is);
//     ByteArrayOutputStream baos = new ByteArrayOutputStream();
//     ImageIO.write(bi, "png",
baos);
//     byte[] bytes = baos.toByteArray();
 
       // 图片转成二进制 将二进制存入数据库
       se.setSoftImage(myfile.getBytes());
      
       softService.insert(se);
 
       model.put("info",
"应用创建成功!");
       log.info("日志:应用创建成功");
 
       //baos.close();
       //is.close();
 
       se = new SoftEntity();
       return search(model, se, request, response);
    }
 

4.     页面展示逻辑

Jsp:

<ul>
                                  <c:forEach
items="${softList }"
var="soft"
varStatus="status"
begin="0">
                                       <li><
4000
/span>
                                            
<a href="${soft.softUrl }">

                                                 
<img src="${pageContext.request.contextPath}/user/showImage.do?index=${status.index}"
                                                 
width="130px"
height="130px"><br
/> <span
                                                 
style="font-size: 16px;font-weight:
bold;color:navy; ">   ${soft.softName}系统</span>
                                            
</a>
                                       </li>
                                  </c:forEach>
                            </ul>

注意上面红色加粗内容src,<img>的src属性可以指定请求,本demo就是当页面加载时带着不同参数,循环去执行请求,请求的响应就是流,如下:

从数据库读取出二进制数据……

byte[] bb = list.get(index).getSoftImage();

 

           // 禁止图像缓存。
response.setHeader("Pragma",
"no-cache");
           response.setHeader("Cache-Control",
"no-cache");
           response.setDateHeader("Expires", 0);
           response.setContentType("image/png");
 
           // 将图像输出到Servlet输出流中
           ServletOutputStream sos =
response.getOutputStream();
           sos.write(bb, 0, bb.length);
          
//同上传,jdk1.6以上支持
           //ImageIO.write(img, "png", sos);
           log.info("日志:用户展现首页成功");
           sos.close();

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DB 图片流