从数据库读图片并在页面中显示

主要思路:

   <img src="showImage.do?name=mall&column=image"/>通过src访问servlet,由response的流输出图片到页面。

流的方式一:

Java代码   收藏代码
  1.      response.setHeader("Cache-Control""no-store");  
  2.      response.setHeader("Pragma""no-cache");  
  3.      response.setDateHeader("Expires"0);  
  4.      response.setContentType("image/jpg");  
  5.      ServletOutputStream responseOutputStream = response.getOutputStream();  
  6.          String name = request.getParameter("mall_name");  
  7.          PreparedStatement ps = null;  
  8.          ResultSet rs = null;  
  9.          Connection con;  
  10.          String sql = "select MALL_LOGO from trustsg_mall_info where MALL_NAME = ?";  
  11. try {  
  12.     Class.forName("com.mysql.jdbc.Driver");  
  13.     con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ofbiz","ofbiz","ofbiz");  
  14.         ps = con.prepareStatement(sql);  
  15.            ps.setString(1, name);  
  16.            rs = ps.executeQuery();  
  17.            if(rs.next()){  
  18.              Blob b = rs.getBlob("MALL_LOGO");  
  19.              Debug.logInfo("showimage"+b, "sssssssssssss");  
  20.                 byte[] images = b.getBytes(1, (int)b.length());  
  21.                 responseOutputStream.write(images);  
  22.            }  
  23.             
  24. catch (SQLException e) {  
  25.     e.printStackTrace();  
  26. catch (ClassNotFoundException e) {  
  27.     e.printStackTrace();  
  28. }    
  29.          responseOutputStream.flush();  
  30.          responseOutputStream.close();  

 流的方式二:

Java代码   收藏代码
  1.  int id= Integer.parseInt(request.getParameter("picid"));  
  2.    String sql = "select pic from p WHERE picid="+id;   
  3.    ResultSet rs=conn.getResult(sql);   
  4. while(rs.next())   
  5. {  
  6.        ServletOutputStream sout = response.getOutputStream();  
  7.        //图片输出的输出流  
  8.        InputStream in = rs.getBinaryStream(1);  
  9.        byte b[] = new byte[0x7a120];  
  10.        for(int i = in.read(b); i != -1;)  
  11.        {  
  12.          sout.write(b);   
  13.          //将缓冲区的输入输出到页面  
  14.          in.read(b);  
  15.        }  
  16.        sout.flush();  
  17.        //输入完毕,清除缓冲  
  18.        sout.close();  

 使用jsp:

Java代码   收藏代码
  1. <%@ page contentType="text/html; charset=gbk" %>   
  2. <%@ page import="java.io.*"%>   
  3. <%@ page import="java.sql.*, javax.sql.*" %>   
  4. <%@ page import="java.util.*"%>   
  5. <%@ page import="java.math.*"%>   
  6.   
  7. <%   
  8. String photo_no = request.getParameter("photo_no");   
  9.   
  10. //mysql连接   
  11. Class.forName("com.mysql.jdbc.Driver").newInstance();   
  12. String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111";   
  13. Connection con = DriverManager.getConnection(URL);   
  14.   
  15. //oracle连接   
  16. //String URL="jdbc:oracle:thin@localhost:1521:orcl2";   
  17. //user="system";   
  18. //password="manager";   
  19. //Connection con = DriverManager.getConnection(URL,user,password);   
  20.   
  21. try{   
  22. // 准备语句执行对象   
  23. Statement stmt = con.createStatement();   
  24.   
  25. String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no;   
  26. ResultSet rs = stmt.executeQuery(sql);   
  27. if (rs.next()) {   
  28. Blob b = rs.getBlob("photo_image");   
  29. long size = b.length();   
  30. //out.print(size);   
  31. byte[] bs = b.getBytes(1, (int)size);   
  32. response.setContentType("image/jpeg");   
  33. OutputStream outs = response.getOutputStream();   
  34. outs.write(bs);   
  35. outs.flush();   
  36. rs.close();   
  37. }   
  38. else {   
  39. rs.close();   
  40. response.sendRedirect("./images/error.gif");   
  41. }   
  42. }   
  43. finally{   
  44. con.close();   
  45. }   

你可能感兴趣的:(java,sql,数据库,servlet,jdbc,ITeye)