数据库类型为BLOB图片,前台显示问题。

来源:互联网 发布:海外运营知乎 编辑:程序博客网 时间:2024/06/06 03:59


BLOB 数据类型,现在已经比较少的出现了,这样设计使数据库冗余。

此处为使用Spring Mvc +Mybatis 技术。

查询BLOB数据我使用直接查询一个实体类,实体类对应的数据类型为Byte[] 数组。


前台页面查看时候,给后台传递一个参数,此处传Id 获取到当前这个实体类,取出Byte[] 数组类型的数据。


此处为Action

@RequestMapping("getImg")public Object getImg(HttpServletRequest request,String id,HttpServletResponse response) throws IOException{        (实体类)Image l=ImageService.get(id); response.setContentType("image/png");   OutputStream output = response.getOutputStream();   ByteArrayInputStream in = new ByteArrayInputStream(l.getImg());//获取实体类对应Byte int len;   byte[] buf = new byte[1024];   while ((len = in.read(buf)) != -1) {      output.write(buf, 0, len);   }   output.flush();  output.close();return null;}

test测试,jsp内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><html><head></head><body><h1> 支付凭证</h1><img src="getImg.action?id=0000000010"/></body></html>

菜鸟记录,以免忘记。

1 0
原创粉丝点击