我正在使用 ADF,需要在轮播中显示图像。已在 servlet 中编写了以下代码,但在页面加载时,如果数据库中存在该特定记录的图像,则将值“weblogic.jdbc.wrapper.Blob_oracle_sql_BLOB@xxx” (xxx - 某个字母数字值)分配给 blob 对象取来的
链接参考 - http://www.baigzeeshan.com/2010/10/display-images-in-carousel-item-in.html
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
String imageId = request.getParameter("id");
OutputStream os = response.getOutputStream();
Connection conn = null;
try {
Context ctx = new InitialContext();
//Datasource as defined in <res-ref-name> element of weblogic.xml
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/HrXEDS");
conn = ds.getConnection();
PreparedStatement statement =
conn.prepareStatement("SELECT image_id, image_blob " +
"FROM hr_image_table " +
"WHERE image_id = ?");
statement.setInt(1, new Integer(imageId));
ResultSet rs = statement.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("IMAGE_BLOB");
System.out.println("-- blob --" + blob);
BufferedInputStream in =
new BufferedInputStream(blob.getBinaryStream());
int b;
byte[] buffer = new byte[10240];
while ((b = in.read(buffer, 0, 10240)) != -1) {
os.write(buffer, 0, b);
}
os.close();
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
System.out.println("SQLException error");
}
}
}