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

主要思路:

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

流的方式一:

        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpg");
        ServletOutputStream responseOutputStream = response.getOutputStream();
            String name = request.getParameter("mall_name");
            PreparedStatement ps = null;
            ResultSet rs = null;
            Connection con;
            String sql = "select MALL_LOGO from trustsg_mall_info where MALL_NAME = ?";
			try {
				Class.forName("com.mysql.jdbc.Driver");
				con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ofbiz","ofbiz","ofbiz");
					ps = con.prepareStatement(sql);
		            ps.setString(1, name);
		            rs = ps.executeQuery();
		            if(rs.next()){
		            	 Blob b = rs.getBlob("MALL_LOGO");
		            	 Debug.logInfo("showimage"+b, "sssssssssssss");
				            byte[] images = b.getBytes(1, (int)b.length());
				            responseOutputStream.write(images);
		            }
		           
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}  
            responseOutputStream.flush();
            responseOutputStream.close();

 流的方式二:

 int id= Integer.parseInt(request.getParameter("picid"));
   String sql = "select pic from p WHERE picid="+id; 
   ResultSet rs=conn.getResult(sql); 
while(rs.next()) 
{
       ServletOutputStream sout = response.getOutputStream();
       //图片输出的输出流
       InputStream in = rs.getBinaryStream(1);
       byte b[] = new byte[0x7a120];
       for(int i = in.read(b); i != -1;)
       {
         sout.write(b); 
         //将缓冲区的输入输出到页面
         in.read(b);
       }
       sout.flush();
       //输入完毕,清除缓冲
       sout.close();

 使用jsp:

<%@ page contentType="text/html; charset=gbk" %> 
<%@ page import="java.io.*"%> 
<%@ page import="java.sql.*, javax.sql.*" %> 
<%@ page import="java.util.*"%> 
<%@ page import="java.math.*"%> 

<% 
String photo_no = request.getParameter("photo_no"); 

//mysql连接 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111"; 
Connection con = DriverManager.getConnection(URL); 

//oracle连接 
//String URL="jdbc:oracle:thin@localhost:1521:orcl2"; 
//user="system"; 
//password="manager"; 
//Connection con = DriverManager.getConnection(URL,user,password); 

try{ 
// 准备语句执行对象 
Statement stmt = con.createStatement(); 

String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no; 
ResultSet rs = stmt.executeQuery(sql); 
if (rs.next()) { 
Blob b = rs.getBlob("photo_image"); 
long size = b.length(); 
//out.print(size); 
byte[] bs = b.getBytes(1, (int)size); 
response.setContentType("image/jpeg"); 
OutputStream outs = response.getOutputStream(); 
outs.write(bs); 
outs.flush(); 
rs.close(); 
} 
else { 
rs.close(); 
response.sendRedirect("./images/error.gif"); 
} 
} 
finally{ 
con.close(); 
} 

你可能感兴趣的:(oracle,sql,mysql,jsp,jdbc)