JSP读取Oracle数据库里的图片Blob字段并显示在页面上

1.java代码:

 
    
 1 /**
 2      * 打印模板获取电子签名
 3      * @param request
 4      * @param resp
 5      * @param id
 6      * @return
 7      * @throws Exception 
 8      */
 9     @RequestMapping(value = "jzgx/getImage", method = RequestMethod.GET)
10     public void getImage(HttpServletRequest request, HttpServletResponse response,String id){
11         String sql = "SELECT SIGNATURE FROM RDP_PSIGNATURE P WHERE P.USER_ID = '"+id+"'";
12         OutputStream os = null;
13         try {
14             RecordSet rs = OperDbUtil.getRecordSet(sql);
15             os = response.getOutputStream();
16             //设定输出文件头
17             response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(id,"UTF-8"));
18             //设置响应头
19             response.setContentType("image/"+id+";charset=utf-8");
20             if(rs.next()){
21                 byte[] buf = rs.getBlob(1);
22                 if(buf == null) return;
23                 //清空输出流    
24                 response.reset();            
25                 os.write(buf);
26             }
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30         //释放资源
31         finally{
32             try {
33                 os.close();
34             } catch (IOException e) {
35                 e.printStackTrace();
36             }
37         }
38     }
 
    

 

 
OperDbUtil是一个我们公司自己写的工具类,没有可以用
1 Connection conn = DBManager.getConnection();3   PreparedStatement ps = null;
4   ResultSet rs = null;
5   InputStream is = null;
6   OutputStream os = null;
7    ps = conn.prepareStatement(sql);
8    rs = ps.executeQuery();

来执行sql语句。

 
 

2.jsp页面:

1 <%
2 String path = request.getContextPath();
3 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
4 %>
5 ...   
6 ...
7 ...
8 

跟网上大多数的不一样,仅供参考。

转载于:https://www.cnblogs.com/wxm-bk/p/6587240.html

你可能感兴趣的:(JSP读取Oracle数据库里的图片Blob字段并显示在页面上)