[學習筆記]java+oracle 存+ jsp 儲圖片到數據庫中 b---讀取

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="cn.java.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.text.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
<title>JSP Page</title>
    
</head>
    
<body>
  
<%

  
             
                Connection conn 
= null;
               Statement stmt
=null;
                ResultSet rs
=null;
            
try {
              
                String sql 
= "select ps from ss where id=15";
                conn 
= DBUtil.getConnection();
               stmt
=conn.createStatement();
         
                rs
=stmt.executeQuery(sql);
             
while(rs.next()){
                 response.setContentType(
"image/jpeg");
                 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);
               }

                 
              
               
//普通數據採用此語句
               //  while(in.read(b)!=-1){ 
                // sout.write(b);//輸出字符流
                /// }

               
                 sout.flush();
                 sout.close();
//關閉sout輸出流
             }

       
        
             
            }
  catch (SQLException ex) {
                out.println(ex.getMessage());
            }


            
finally {
                
try {
                 
                    DBUtil.close(rs, stmt, conn);
                }
 catch (Exception ex) {
               out.println(ex.getMessage());
                }

            }

%>
    
</body>
</html>
發佈了62 篇原創文章 · 獲贊 1 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章