3 Replies Latest reply on Jul 8, 2008 1:50 AM by Jane A

    java.lang.IllegalStateException: Servlet response already us

    Jane A Newbie

      Hi,

      When trying to download an excel file from my application web page, I am getting the above error.

      I am using Tomcat 6.0.16, richfaces-3.2.0.SR1, jsf 1.2 RI, and apache poi-3.0.2 to generate the xls files.

      My code is as follows:

      public void downloadReport()
       {
       FacesContext context = FacesContext.getCurrentInstance();
       HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
      
       int read = 0;
       byte[] bytes = new byte[1024];
      
       response.setContentType("application/vnd.ms-excel");
       response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName +"\"");
      
       FileInputStream fis = null;
       OutputStream os = null;
      
       HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
       try
       {
       fis = new FileInputStream(new File(downloadPath + fileName));
       os = response.getOutputStream();
      
       while((read = fis.read(bytes)) != -1)
       {
       os.write(bytes, 0, read);
       }
      
       os.flush();
       os.close();
      
       }
       catch (FileNotFoundException e)
       {
       e.printStackTrace();
       }
       catch (IOException e)
       {
       e.printStackTrace();
       }
       }




      While searching for an answer to this problem, I have found that may people have encountered the same issue but there has been no solution up to now. Is this a bug inherent in richfaces?

      Please help me!

      Thanks.