/**
* 转载请注明作者longdick http://longdick.iteye.com
*
*/
首先定义一个读取Oracle数据库的Blob字段并把字节写入一个输出流的方法:
public static void writeImg(OutputStream os) {
Connection con;
try {
con = ConnectionFactory.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select pic from blob_table where b_id=1");
byte[] b = new byte[1024];
if (rs.next()) {
Blob blob = rs.getBlob(1);
InputStream is = blob.getBinaryStream();
int i = 0;
while ((i = is.read(b)) != -1) {
os.write(b, 0, i);
}
os.close();
is.close();
}
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
然后做一个servlet,在doGet方法里调用writeImg方法
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBOperator.writeImg(response.getOutputStream());
}
最后只需要在显示的jsp页面里面定义img标签的src属性为servlet的访问地址就可以了:
<img src="servlet/newServlet" >