ShowImage.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package form;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CriminalPic extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "epolicia", "admin");
PreparedStatement ps = con.prepareStatement("select * from CRIMINAL_PIC where CRIMINAL_ID = ?");
String id = request.getParameter("id");
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
rs.next();
Blob b = rs.getBlob("PICTURE");
response.setContentType("image/jpeg");
response.setContentLength( (int) b.length());
InputStream is = b.getBinaryStream();
OutputStream os = response.getOutputStream();
byte buf[] = new byte[(int) b.length()];
is.read(buf);
os.write(buf);
os.close();
}
catch(Exception ex) {
PrintWriter out = response.getWriter();
out.print("../pics/thief.jpg");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
show.jsp
<%out.print("<img width='100%' height='200' src=../CriminalPic?id=" +rs.getString(1) + "></img>");%>
我正在尝试使用Servlet显示数据库中的图像。它工作正常,但是对于替代方案,当它有任何异常时,对于那种情况,它必须显示替代图像,其路径已经给出。但是,它没有显示任何东西。需要帮助!!
答案 0 :(得分:0)
你是否从HTML中的img标签的src属性调用CriminalPic Servlet?
如果是,那么这就是我的想法。
public class CriminalPic extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream is = null;
OutputStream os = response.getOutputStream();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "epolicia", "admin");
PreparedStatement ps = con.prepareStatement("select * from CRIMINAL_PIC where CRIMINAL_ID = ?");
String id = request.getParameter("id");
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
rs.next();
Blob b = rs.getBlob("PICTURE");
response.setContentType("image/jpeg");
response.setContentLength( (int) b.length());
is = b.getBinaryStream();
byte buf[] = new byte[(int) b.length()];
is.read(buf);
os.write(buf);
}
catch(Exception ex) {
File file = new File(getServletContext.().getRealPath("/pics/thief.jpg"));
FileInputStream fis = new FileInputStream(file);
int bytes;
while ((bytes = fis.read()) != -1) {
os.write(bytes);
}
}