【发布时间】:2014-08-02 14:14:51
【问题描述】:
我已尝试使用以下代码检索存储在 Oracle 数据库中的具有 BLOB 数据类型的图像。在 apace tomcat7 服务器上运行它后,我只能看到一个图像图标(可能是损坏的图像),但看不到图像。当我试图打开它被下载的图像图标时。我使用 picasa 打开下载的图像,但显示“图像无效”。 请尝试解决我的问题....!
public class RetrieveInkblot extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Blob photo = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String query = "select img from inkblots where bid = 23";
ServletOutputStream out = response.getOutputStream();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "manager");
out.println("Connection");
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if (rs == null) {
out.print("null except");
} else if (rs.next()) {
photo = rs.getBlob(1);
} else {
response.setContentType("text/html");
out.println("<html><head><title>Person Photo</title></head>");
out.println("<body><h1>No photo found for id= 001 </h1></body></html>");
return;
}
byte[] imgData = photo.getBytes(1, (int) photo.length());
response.setContentType("image/jpg");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
} catch (Exception e) {
response.setContentType("text/html");
out.println("<html><head><title>Error: Person Photo</title></head>");
out.println("<body><h1>Error=" + e.getMessage() + "</h1></body></html>");
return;
}
}
}
【问题讨论】:
-
不要将图像存储在数据库中。只需将图像路径存储在数据库中即可。