Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

Retrieve Image or any document from database using MVC pattern in java

My program is working, but JSP page loading very slow that is my issue. I am passing empid and image name as a parameter from a jsp page to a Servlet which I am able to do perfectly and it is receiving in Servlet and calling data access class associated method, after that I am retrieving it and passing all this to JSP in JSP I am using Scriptlet inside that image is loading into JSP page but it is loading very slow at least 10-15 second and console showing below error :

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 class method

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

Image retrieval servlet which is calling data access class(employee bean) method on the basis of input which we have received from jsp page (image name and emp id name)

   @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 page after calling method where we want to see image response:

 <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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this line in your jsp:-

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

Also do the below change:-

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...