读取数据库的图片
/** * 读取数据库中Blob类型的图片到页面(核心方法) */ public void streamImage(final String advertiseId,final OutputStream contentStream, final HttpServletRequest request)throws DataAccessException{ Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; InputStream in = null; try{ connection = this.getJdbcTemplate().getDataSource().getConnection(); String sql = "select PIC from HOLIDAY_ADVERTISEMENT where ADVERTISEID = ? "; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, advertiseId); resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ Blob blob = resultSet.getBlob(1); if(blob != null){ in = blob.getBinaryStream(); byte[] b = new byte[1024]; int a = in.read(b); while(a!=-1){ contentStream.write(b,0,a); contentStream.flush(); a = in.read(b); } } } }catch (Exception e) { System.out.println("图片下载出错..."); e.printStackTrace(); }finally{ try{ if(resultSet!=null){ resultSet.close(); } if(preparedStatement!=null){ preparedStatement.close(); } if(connection!=null){ connection.close(); } }catch (Exception e) { e.printStackTrace(); } } }<img src="modules/advertisement/AdvertiseManageAction.do?action=showPic&advertiseId=${list.advertiseid }" />
/** * 获取数据库中Blob类型的图片 * @param mapping * @param actionForm * @param request * @param response * @return * @throws Exception */ public ActionForward showPic(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub String advertiseId = request.getParameter("advertiseId"); System.out.println(advertiseId); OutputStream outputStream = response.getOutputStream(); advertiseManageService.streamImage(advertiseId,outputStream, request); return null; }
相关推荐
CoderToy 2020-11-16
技术之博大精深 2020-10-16
emmm00 2020-11-17
bianruifeng 2020-11-16
云中舞步 2020-11-12
世樹 2020-11-11
暗夜之城 2020-11-11
张荣珍 2020-11-12
amienshxq 2020-11-14
ASoc 2020-11-14
yungpheng 2020-10-19
loveyouluobin 2020-09-29
尘封飞扬 2020-09-29
Coder技术文摘 2020-09-29
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09