2 Replies Latest reply on Aug 3, 2012 11:28 PM by Sabarinath Selvaraj

    getOutputStream() has already been called for this response

    Sabarinath Selvaraj Newbie

      Hi

       

      I am using JSF 2.1 and use the following code to down load the file.

       

      Code used in Managed bean :

       

      public void downloadFile() throws Exception

      {

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); 

        Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();    

        this.setFileName(params.get("FileName"));

        this.setFilePath(params.get("FileLocation"));   

        response.setHeader("Content-disposition","attachment;filename="+this.getFileName());

        File file=new File(this.getFilePath(),this.getFileName());

        try {

         sendFile(file,response);

        }catch (Exception exception) {

         throw new Exception (exception.getMessage());

        }

      }

      private void sendFile(File fileOut, HttpServletResponse response) throws Exception

      {

        response.setContentType("application/vnd.ms-excel");

        FileInputStream fis = new FileInputStream(fileOut);

        BufferedInputStream bis = new BufferedInputStream(fis);

        int ch=0;

        byte[] buf = new byte[10000];

        try{

         while((ch = bis.read(buf)) >= 0) {

          response.getOutputStream().write(buf,0,ch);

         }

        }catch (Exception exception) {

         throw new Exception (exception.getMessage());

        }

        finally{

         response.getOutputStream().flush(); 

         response.getOutputStream().close();

         bis.close();

         fis.close();

        }

       

      }

       

      Error :

       

      java.lang.IllegalStateException: getOutputStream() has already been called for this response

             at org.apache.catalina.connector.Response.getWriter(Response.java:614) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:199) [jbossweb-7.0.13.Final.jar:]

             at com.sun.faces.context.ExternalContextImpl.getResponseOutputWriter(ExternalContextImpl.java:723) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at javax.faces.context.ExternalContextWrapper.getResponseOutputWriter(ExternalContextWrapper.java:669) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

             at com.sun.faces.application.view.FaceletViewHandlingStrategy.createResponseWriter(FaceletViewHandlingStrategy.java:938) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:377) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

             at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [jsf-impl-2.1.7-jbossorg-2.jar:]

             at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]

             at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.event(JBossWebContext.java:67)

             at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.invoke(JBossWebContext.java:48)

             at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]

             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]

             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]

             at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]

             at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]

             at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_06]

       

      Thanks in Advance

      Sabarinath

        • 1. Re: getOutputStream() has already been called for this response
          Leandro godoy Newbie

          this is my example , kisses.

          FacesContext facesContext = FacesContext.getCurrentInstance();

                     

                      File file = new File(AnexosUtils.PATH + pasta + File.separator

                              + anexo.getNomeDoArquivo());

                      byte[] bytesFile = getBytesFromFile(file);

           

                      HttpServletResponse response = (HttpServletResponse) facesContext

                              .getExternalContext().getResponse();

                      ServletContext servletcontext = (ServletContext) facesContext

                              .getExternalContext().getContext();

           

                      try {

                          String mimetype = servletcontext.getMimeType(anexo

                                  .getNomeDoArquivo().toLowerCase());

                          int tamanho = bytesFile.length;

           

                          response.setContentType((mimetype != null) ? mimetype

                                  : "application/octet-stream");

                          response.setHeader("Content-Disposition",

                                  "attachment;filename=\"" + anexo.getNomeDoArquivo()

                                          + "\"");

                          response.setContentLength(tamanho);

                          response.getOutputStream().write(bytesFile, 0, tamanho);

                          response.getOutputStream().flush();

           

                          facesContext.responseComplete();

                          facesContext.renderResponse();

          1 of 1 people found this helpful
          • 2. Re: getOutputStream() has already been called for this response
            Sabarinath Selvaraj Newbie

            Hi Leandro godoy,

             

            Thanks for your reply .

             

            facesContext.responseComplete();

            facesContext.renderResponse();

             

            After adding these two lines its working fine.

             

            Regards,

            Sabarinath

            1 of 1 people found this helpful