JSP Oracle BLOB 图片读取

来源:互联网 发布:网络安全技术有哪些 编辑:程序博客网 时间:2024/06/04 19:13
  1. <%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  6. <title>无标题文档</title>
  7. </head>
  8. <body>
  9. <h2>显示图片:</h2>
  10. <%@ page import="java.io.*"%> 
  11. <%
  12.     try
  13.     {
  14.         Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
  15.         String url="jdbc:oracle:thin:@localhost:1521:ORCL";   
  16.         Connection con=DriverManager.getConnection(url,"system","oracle"); 
  17.         con.setAutoCommit (false);
  18.         Statement   stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
  19.         ResultSet rs=stmt.executeQuery("select id,txt,imag from hr.psn where id=(select max(id) from hr.psn)");//
  20.         
  21.         if(rs.next())
  22.         {
  23.             int len = 10 * 1024 * 1024;
  24.             InputStream in = (InputStream)rs.getBinaryStream("imag");//① 
  25.             response.reset(); //返回在流中被标记过的位置 
  26.             response.setContentType("image/jpg"); //或gif等 //得到输入流 
  27.             OutputStream toClient = response.getOutputStream();//② 
  28.             byte[] P_Buf = new byte[len]; 
  29.             int i=-1; 
  30.             while ((i = in.read(P_Buf)) != -1)
  31.             {
  32.              toClient.write(P_Buf, 0, i);
  33.             } 
  34.             in.close(); 
  35.             toClient.flush(); //强制清出缓冲区 
  36.             toClient.close();// 
  37.             con.commit();
  38.         }
  39.         
  40.         rs.close(); 
  41.         stmt.close();
  42.         con.close();    
  43.     }
  44.     catch(Exception   ex)
  45.     {     
  46.         ex.printStackTrace(); 
  47.     }
  48. %>
  49. </body>
  50. </html>
原创粉丝点击