我想在 JSP 页面中显示 MySQL 表中的 blob(图像),我的代码如下:
<%
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
byte[ ] imgData = null ;
String DRIVER= "com.mysql.jdbc.Driver";
String databaseName = "imd";
String connectionUrl = "jdbc:mysql://127.0.0.1:3306/" + databaseName;
String DB_USER = "root";
String DB_PASSWD = "root";
try{
Class.forName(DRIVER);
con = DriverManager.getConnection(connectionUrl,DB_USER,DB_PASSWD);
stmt = con.createStatement();
rs = stmt.executeQuery("select SiteKey from imd_user_sitekey where userName = 'lili'");
if (rs.next()) {
image = rs.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
} else {
out.println("Display Blob Example");
out.println("image not found for given id>");
return;
}
response.setContentType("image/gif");
OutputStream out = response.getOutputStream();
out.write(imgData);
out.flush();
oout.close();
}catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
<h1>Hello, <%= message %>, Please enter your password!</h1>
<br/>
<html:form action="sitekey">
<bean:message key="label.password"/>
<html:password property="password"></html:password>
<html:submit/>
</html:form>
图片可以显示成功,但是我的JSP页面中<%%>代码下面的其他内容没有显示出来,整个页面就像一张图片一样。有人可以帮我弄清楚这个吗?非常感谢!