做了一个JSP直接显示Oracle的Blob类型图片的小功能,参考代码如下:
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="net.sysmain.common.ConnectionManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
response.reset();
String guid = request.getParameter("guid");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try{
conn = ConnectionManager.getInstance().getConnection();
ps = conn.prepareStatement("select img from user where guid=?");
ps.setString(1, guid);
rs = ps.executeQuery();
if(rs.next()){
is = rs.getBinaryStream(1);
}
response.setContentType("image/jpeg");
os = response.getOutputStream();
int num;
byte buf[] = new byte[1024];
while( (num=is.read(buf))!=-1 ){
os.write(buf, 0, num);
}
os.flush();
is.close();
os.close();
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.close(conn,ps,rs);
}
%>