日期:2014-05-16 浏览次数:20569 次
上传一张图片,以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;
??? }
?
呵呵,很简单吧!