1 Reply Latest reply on Feb 3, 2008 6:08 PM by Pete Muir

    Error when downloading file to browser

    Todd Nash Newbie

      I would really appreciate some answers on this one...

      I have a page that allows the user to download a file that is stored in the database as a BLOB. The following exception is thrown when the download link is clicked. I can trace through my method to the point where the reponse is returning through the Seam filters, so I know that the method is returning the bytes associated with the file.

      java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
       at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:226)
       at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
       at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
       at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
       at com.sun.facelets.FaceletViewHandler.createResponseWriter(FaceletViewHandler.java:414)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:571)
       at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
       at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:216)
       at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
       at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
       at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
      ...


      My page looks like this:

      <h:panelGrid id="document" columns="2">
       <h:outputLabel value="Location"/>
       <h:outputText value="#{documentLocation.location}"/>
      
       <h:outputLabel value="Upload Date"/>
       <h:outputText value="#{documentLocation.uploadDate}"/>
      
       <h:outputLabel value="Content Type"/>
       <h:outputText value="#{documentLocation.contentType}"/>
      
       <h:outputLabel value="Description"/>
       <h:outputText value="#{documentLocation.notes}"/>
      
       <h:panelGroup>
       <s:link id="downloadDocument" value="Download"
       action="#{documentManager.onDownload}"
       view="/download.xhtml">
       <f:param name="docId" value="#{documentLocation.id}"/>
       </s:link>
       </h:panelGroup>
       </h:panelGrid>
      
      


      The onDownload method looks like this:
      public String onDownload()
       {
       System.out.println( "DocumentManagerAction.onDownload() called" );
       System.out.println( "Value of doc id is " + docId );
      
       HttpServletResponse response = ( HttpServletResponse ) facesContext.getExternalContext().getResponse();
      
       response.setContentType( "application/pdf" );
       try
       {
       byte[] docData = getDocumentData();
      
       if ( docData != null && docData.length > 0 )
       {
       response.setContentLength( docData.length );
       os.write( docData );
       os.flush();
       os.close();
      
      
       System.out.println( "\nSuccess\n" );
       }
       facesContext.responseComplete();
      
       }
       catch ( Exception e )
       {
       System.out.println( "\nFailure : " + e.toString() + "\n" );
       return null;
       }
      
       return "done";
       }