jsp页面显示数据库中的二进制保存的图片及下载

hehezhou 2012-02-10

上传一张图片,以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());

相关推荐