jsp页面显示数据库中的二进制保存的图片及下载
上传一张图片,以Blob的形式保存进数据库。保存进之后如何在页面显示这张图片呢?
废话少说,直接上代码。
jsp页面代码:
<img src="/webusermng/userh/showuserhbusinesslicense.do?hbusinesslicenseId=<%=userHBusinessLicense[0][1]%>" width="77" height="93" style="cursor: pointer;" onmouseover="javascript:document.getElementById('divimglicense').style.display='';" onmouseout="javascript:setTimeout(function(){document.getElementById('divimglicense').style.display='none'}, 1000);"/>
对了,你猜对了。其实就是一个image的src请求:
<img src="/webusermng/userh/showuserhbusinesslicense.do?hbusinesslicenseId=<%=userHBusinessLicense[0][1]%>" />
后台如何处理呢?
private ActionForward showUserHBusinessLicense(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws BizException { String hbusinesslicenseId = request.getParameter("hbusinesslicenseId"); if(hbusinesslicenseId == null || "".equals(hbusinesslicenseId)) { throw new BizException("营业执照编号不正确!"); } try { Web_file web_file = serviceLocator.getUserHService().queryHBusinessLicenseImage(Integer.valueOf(hbusinesslicenseId)); if(web_file != null) { byte[] buf = web_file.getUpf_context();// 获取Blob字节数组 response.setContentType(web_file.getUpf_contexttype()); OutputStream hblicense = response.getOutputStream();// 获取输出流 for(int i = 0; i < buf.length; i++) { hblicense.write(buf[i]);// 输出到页面 } hblicense.close();// 关闭输出流 } return null; } catch(Exception e) { e.printStackTrace(); return null; } }
这儿最主要的就是流的读出:
OutputStream hblicense = response.getOutputStream();// 获取输出流
for(int i = 0; i < buf.length; i++)
{
hblicense.write(buf[i]);// 输出到页面
}
hblicense.close();// 关闭输出流
看一看serivce的queryHBusinessLicenseImage实现层如何处理的
public Web_file queryHBusinessLicenseImage(Integer upf_id) throws BizException { SQLHelper sqlExec = new SQLHelper(false); Web_file web_file = null; try { sqlExec_lietou.beginTrans(); web_file = new Web_file(); web_file.setUpf_id(upf_id); web_file = (Web_file) ORMHelper.getObjectByPK(sqlExec, web_file); } catch(Exception e) { e.printStackTrace(); } finally { sqlExec_lietou.close(); } return web_file; }
呵呵,很简单吧!
另外一种上传附件,下载另存为附件的方法。
@RequestMapping(value = "/downloadcandidateannex/", method = RequestMethod.GET) public void downLoadCandidateAnnex(HttpServletRequest request, HttpServletResponse response) throws Exception { Integer userh_id = this.getCurrentUserId(request); if(userh_id == null || userh_id <= 0) { throw new BizException("用户编号不正确!"); } String webfileId = request.getParameter("webfileId"); WebFileForm webFileForm = serviceFactory.getPcdetailService().getCandidateAnnex(userh_id, webfileId); response.setContentType("application/x-msdownload"); try { String fileName = new String(webFileForm.getWeb_file().getUpf_filename().getBytes("gbk"), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment;" + " filename=" + fileName); InputStream ips = serviceFactory.getMongoDBService().getFileInputStream(IMongoDBService.TABLE_WEB_FILE, Integer.valueOf(webfileId)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b = 0; while((b = ips.read()) != -1) baos.write(b); response.getOutputStream().write(baos.toByteArray()); } catch(Exception e) { throw new BizException("文件下载出错!", e); } }
一:设置下载头信息response.setContentType("application/x-msdownload");
二:获取文件流,读出
InputStream ips = serviceFactory.getMongoDBService().getFileInputStream(IMongoDBService.TABLE_WEB_FILE, Integer.valueOf(webfileId)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b = 0; while((b = ips.read()) != -1) baos.write(b); response.getOutputStream().write(baos.toByteArray());
相关推荐
hehezhou 2012-02-10
jiangtie 2020-08-15
长安长夜Saint 2020-06-14
kerson 2020-05-11
milidou 2020-01-11
paopao00 2020-01-09
Joymine 2020-01-06
疯仔嵌入式 2011-04-10
wordmhg 2019-10-24
Hashxu 2012-04-21
benico 2010-01-31
MySQLfred 2019-07-01
hanniuniu 2019-06-27
frank0 2010-03-30
Andyshrk 2018-08-23
一只菜鸡 2014-05-28
dalian 2010-03-15
Stephenzsky 2019-05-27
qimingstack 2015-09-25