- <%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- </head>
- <body>
- <h2>显示图片:</h2>
- <%@ page import="java.io.*"%>
- <%
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
- String url="jdbc:oracle:thin:@localhost:1521:ORCL";
- Connection con=DriverManager.getConnection(url,"system","oracle");
- con.setAutoCommit (false);
- Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- ResultSet rs=stmt.executeQuery("select id,txt,imag from hr.psn where id=(select max(id) from hr.psn)");//
- if(rs.next())
- {
- int len = 10 * 1024 * 1024;
- InputStream in = (InputStream)rs.getBinaryStream("imag");//①
- response.reset(); //返回在流中被标记过的位置
- response.setContentType("image/jpg"); //或gif等 //得到输入流
- OutputStream toClient = response.getOutputStream();//②
- byte[] P_Buf = new byte[len];
- int i=-1;
- while ((i = in.read(P_Buf)) != -1)
- {
- toClient.write(P_Buf, 0, i);
- }
- in.close();
- toClient.flush(); //强制清出缓冲区
- toClient.close();//
- con.commit();
- }
- rs.close();
- stmt.close();
- con.close();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- %>
- </body>
- </html>
JSP Oracle BLOB 图片读取
最新推荐文章于 2021-04-11 20:35:25 发布