jsp中读取oracle数据库中blob图片并显示的问题(附源码)
showimage.jsp
<%@ page ContentType="text/html; charset=gb2312" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.io.*" %>
<%@ include file = "dblib.jsp" %>
<%
Connection con = null;
ResultSet rs = null;
String sql = "";
byte[] blocco;
//获得与数据库的连接
con = getConnection();
sql = "select image from tImage where imageid = 1";
if ( con != null ) {
rs = executeQuery(con,sql);
if ( rs.next() ) {
blocco = rs.getBytes("image");
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
for ( int i = 0 ; i < blocco.length ; i++ ) {
op.write(blocco[i]);
}
op.close();
}
rs.close();
}
con.close();
%>
dblib.jsp
<%@ page import="java.sql.*" %>
<%!
boolean VERBOSE = true;
public void log(String s) {
if (VERBOSE) System.out.println("==>do register log:" + s);
}
public Connection getConnection() {
try {
String jdbcDriver = "oracle.jdbc.driver.OracleDriver";
String dbURL = "jdbc:oracle:oci8:@wfserver";
Class.forName(jdbcDriver).newInstance();
return DriverManager.getConnection(dbURL, "zyb", "zyb");
} catch ( Exception e ) {
log("create con exception:" + e.toString());
return null;
}
}
public ResultSet executeQuery(Connection con, String sql) {
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
return ps.executeQuery();
} catch ( Exception e ) {
log("execute query error e: " + e.toString ());
return null;
}
}
%>