1.java代码:
/**
* 打印模板获取电子签名
* @param request
* @param resp
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value = "jzgx/getImage", method = RequestMethod.GET)
public void getImage(HttpServletRequest request, HttpServletResponse response,String id){
String sql = "SELECT SIGNATURE FROM RDP_PSIGNATURE P WHERE P.USER_ID = '"+id+"'";
OutputStream os = null;
try {
RecordSet rs = OperDbUtil.getRecordSet(sql);
os = response.getOutputStream();
//设定输出文件头
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(id,"UTF-8"));
//设置响应头
response.setContentType("image/"+id+";charset=utf-8");
if(rs.next()){
byte[] buf = rs.getBlob(1);
if(buf == null) return;
//清空输出流
response.reset();
os.write(buf);
}
} catch (Exception e) {
e.printStackTrace();
}
//释放资源
finally{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OperDbUtil是一个我们公司自己写的工具类,没有可以用
Connection conn = DBManager.getConnection(); PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
来执行sql语句。
2.jsp页面:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
...
...
...
<td><img src="<%=basePath%>jzgx/getImage.sd?id=${user1.id}" /></td>
跟网上大多数的不一样,仅供参考。