首页 > 代码库 > blob数据输出到页面显示图片

blob数据输出到页面显示图片

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://www.mamicode.com/">
    
    <title>My JSP ‘showImg.jsp‘ starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
	-->

  </head>
  
  <body>
    <table>
      <tr>
       <td>111</td>
       <td><img  src=http://www.mamicode.com/‘#/‘" /showimg.do?id=1" width="200px" height="180px" ></td>
      </tr>
    </table>
    
  </body>
</html>
@RequestMapping(value = "/uploadimage.do")  
	    public void uploadPhoto(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception {
			try{
				byte[] data = file.getBytes();
	            int id = uploadDao.FileUpload(data, file.getOriginalFilename());
			}
			catch(Exception e){
				e.printStackTrace();
			}

		}
	   
	    @RequestMapping(value = "/showimg.do")  
	    public void showimg(HttpServletRequest request,HttpServletResponse response) throws Exception {
	    	int id = Integer.parseInt(request.getParameter("id"));
	    	System.out.println(id);
	    	List<Object>  list =  uploadDao.ShowImg(id);
	    	Blob blob=(Blob) list.get(0);
	    	int length = (int) blob.length();
	    	byte[] bImage = new byte[length];
	    	InputStream is = new BufferedInputStream(blob.getBinaryStream());
	    	is.read(bImage, 0, length);
	    	OutputStream out = response.getOutputStream(); 
	    	out.write(bImage);
	    	out.flush(); 
	    	out.close();
	    	is.close();
		}
	
public List<Object> ShowImg(int id) {
		String sql = "select name,img from img where id = "+id+"";
		List<Object> list=new ArrayList<Object>();
		Connection conn=null;
		Statement state=null;
		ResultSet rs = null;
	       try {
				conn = jdbcTemplate.getDataSource().getConnection();
			    state=conn.createStatement();
			    rs=state.executeQuery(sql.toString());
			    if (rs.next()) {
					Blob blob = rs.getBlob("img");
					list.add(0, blob);
					
					    }
	       } catch (SQLException e) {
	    	   	e.printStackTrace();
	       }
	       return list;
	}


本文出自 “骑猴上树” 博客,请务必保留此出处http://qihoushangshu.blog.51cto.com/7872138/1543647

blob数据输出到页面显示图片