一尘不染

使用Java中的MVC模式从数据库中检索图像或任何文档

jsp

我的程序正在运行,但是JSP页面加载非常缓慢,这是我的问题。我将从一个jsp页面中将Empid和图像名称作为参数传递给一个Servlet,我能够完美地完成此操作,并且它在Servlet中接收并调用数据访问类关联的方法,之后我将其检索并将所有这些传递给JSP在JSP中,我正在使用Scriptlet,该映像正在加载到JSP页面中,但是加载速度非常慢,至少10-15秒,并且控制台显示以下错误:

EVERE: Servlet.service() for servlet [jsp] in context with path [/MVCDemoProject] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this   response

EmployeeBean类方法

  public Employee RetrieveImg(Employee emp1){
  Connection con = null;
  Statement stmt = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  byte[] sImageBytes;
  int z = emp1.getEmpId();
  String q = emp1.getIname();
  try {
   int count =0;
     con = ConnectionManager.getConnection();
     stmt = con.createStatement();
     String Query ="SELECT photo FROM upload_documets WHERE empId ='"+z+"' and Name ='"+q+"'";
     rs = stmt.executeQuery(Query);
           if(rs.next()) 
       {          sImageBytes = rs.getBytes(1);
                  emp1 = new Employee();
                  emp1.setFileBytes(sImageBytes);
               emp1.setValid1(true);     
       } 
  }catch (SQLException  ex) {

       } finally {
           try {
               if (stmt != null) {
                   stmt.close();
               }
               if (con != null) {
                   con.close();
               }
           } catch (SQLException ex) {
            Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
    return emp1;
   }

图像检索servlet,它基于我们从jsp页面收到的输入(图像名称和emp id名称)调用数据访问类(员工bean)方法

   @WebServlet(name = "Retrieve_Image", urlPatterns = {"/Retrieve_Image"})
   public class Retrieve_Image extends HttpServlet {
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession(true); 
Employee emp1 = (Employee)session.getAttribute("emp1");

    String iname = request.getParameter("iname");
    emp1.setEmpId(emp1.getEmpId());
    emp1.setIname(iname);
    EmployeeBean eb = new EmployeeBean();
    eb.RetrieveImg(emp1);
    if(emp1.isValid1())
    {
         response.sendRedirect("Image.jsp");
    }

}

@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);
}

@Override
public String getServletInfo() {
    return "Short description";
  }
 }

调用方法后我们要查看图像响应的jsp页面:

 <body>

 <%
Employee emp1 = (Employee)session.getAttribute("emp1");
 session.setAttribute("emp1",emp1);

   byte[] sImageBytes;
   try{
  sImageBytes = emp1.getFileBytes();

  response.setContentType("image/jpeg");
   response.setContentLength(sImageBytes.length);
 response.setHeader("Content-Disposition", "inline; filename=\"");

 BufferedInputStream  input = new BufferedInputStream
 (new ByteArrayInputStream(sImageBytes));
    BufferedOutputStream output =
   new BufferedOutputStream(response.getOutputStream());

  byte[] buffer = new byte[8192];
   int length;
    while ((length = input.read(buffer)) > 0) {
 output.write(buffer, 0, length);
}
 }
  catch(Exception ex){
  System.out.println("error :"+ex);
    }
 %>

 </body>
  </html>

阅读 242

收藏
2020-06-10

共1个答案

一尘不染

在您的jsp中添加以下行:

<%@page language="java" trimDirectiveWhitespaces="true"%>

同时进行以下更改:-

BufferedInputStream  input;
 BufferedOutputStream output;

........

   try{
       .........
  ............

    BufferedInputStream  input = new BufferedInputStream
     (new ByteArrayInputStream(sImageBytes));
        BufferedOutputStream output =
       new BufferedOutputStream(response.getOutputStream());

      int length;
      length = sImageBytes.length;    
      output.write(sImageBytes, 0, length);
    catch(Exception ex){
         System.out.println("error :"+ex);
    } finally{
    if(output != null) {
       output.flush();
       output.close();
    }
    if(input != null) {
       input.close();
    }
2020-06-10