readblob.jsp <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*, javax.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%> <!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> <% java.sql.Connection conn; ResultSet rs=null; Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test","root",""); Statement stmt=conn.createStatement(); rs=stmt.executeQuery("select * from test where id='1'"); if(rs.next()) { Blob b = rs.getBlob("pic"); int size =(int)b.length(); out.print(size); InputStream in=b.getBinaryStream(); byte[] by= new byte[size]; response.setContentType("image/jpeg"); ServletOutputStream sos = response.getOutputStream(); int bytesRead = 0; while ((bytesRead = in.read(by)) != -1) { sos.write(by, 0, bytesRead); } in.close(); sos.flush(); } %> </body> </html> ******************************************************************** 注意:在用sos.write(by, 0, bytesRead);时,该方法把inputstream中的内容在一个新的页面中输出, 如果本页中还有别的内容要输出的话,只有把上述方法改为,bytesRead = in.read(by)) ; 再用out.print(new String(by));方法输出结果,注意在这里不能用by.toString()方法,该方法返回的是要输出内容的内存地址。mysql中有blob textarea类型大小了66536基本上放点小的东东就足够了,哈哈,但是现在的数码pic越来越大就只能用longblob了 大小有4g能 ,放个电影都够了哈哈 |