如何向数据库中存取BLOb字段

存: 创建一个和数据对应的实体pojo: 其中数据库blob字段对应的实体属性定义成byte[]类型 fileContant,然后得到上传的file后getBytes()给fileContant赋值,直接入库则OK.
取:
@RequestMapping(value = "/loadAttach", method = RequestMethod.GET)
	public void loadAttach(HttpServletRequest request,
			HttpServletResponse response,Attach attach) throws Exception{
		Cp cp=(Cp) request.getSession().getAttribute("cp");
		if(StringUtil.isNotEmpty(cp.getBizLicenceImg())||StringUtil.isNotEmpty(cp.getBankLicenceImg())||StringUtil.isNotEmpty(cp.getTaxNoImg())){
			 attach=service.load(attach);
			 InputStream input=new ByteArrayInputStream(attach.getAttachcontent());   
			  byte[] buffer=new byte[input.available()];
			  ServletOutputStream out=response.getOutputStream();
			  int length=0;
			  while((length=input.read(buffer))!=-1){
			   out.write(buffer,0,length);
			  }
			  out.flush();
			  out.close();
		}
	}


其中attach是对应的实体
在jsp页面显示:如果是图片则<img src="相应的action路径">,不是图片则用<a href="相应action路径">下载<a>

你可能感兴趣的:(jsp)