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>
菜鸟记录,以免忘记。