`
阅读更多
/**
	 * 读取数据库中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;
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics