首页 > 代码库 > JSP读取Oracle数据库里的图片Blob字段并显示在页面上<转>

JSP读取Oracle数据库里的图片Blob字段并显示在页面上<转>

java代码:首先定义一个读取Oracle数据库的Blob字段并把字节写入一个输出流的方法。
1
public static void writeImg(OutputStream os) { 2 Connection con; 3 try { 4 con = ConnectionFactory.getConnection(); 5 Statement stmt = con.createStatement(); 6 ResultSet rs = stmt.executeQuery("select pic from blob_table where b_id=1"); 7 byte[] b = new byte[1024]; 8 if (rs.next()) { 9 Blob blob = rs.getBlob(1); 10 InputStream is = blob.getBinaryStream(); 11 int i = 0; 12 while ((i = is.read(b)) != -1) { 13 os.write(b, 0, i); 14 } 15 os.close(); 16 is.close(); 17 } 18 rs.close(); 19 stmt.close(); 20 con.close(); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 }

然后做一个servlet,在doGet方法里调用writeImg方法
1
public void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 4 DBOperator.writeImg(response.getOutputStream()); 5 6 }

 

jsp代码:

<img src=http://www.mamicode.com/"servlet/newServlet"  >  

转载:作者longdick    http://longdick.iteye.com

JSP读取Oracle数据库里的图片Blob字段并显示在页面上<转>