title: Download a file using getOutputStream() and bug when rendering the view that calls getWriter()()
body: <p>I am currently stuck, I need to download a file in a web page, it works, but I always get an exception.</p>
  
  <p>I use getOutputStream() because I think that i cannot use getWriter() to write in a byte array. I am still not very sure about closing or not the outputstream, I have seen a lot of different opinions about it.</p>
  
  <pre><code>public void downloadFile() {
      int statementNumber = Integer.parseInt(
              FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("statementNumber"));
      MerchantStatementDocument document = findDocumentFromStatementNumber(statementNumber);
      HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
              .getResponse();
  
      response.setHeader("Content-Disposition", "attachment;filename=" + document.getDocumentName());
      response.setContentType("application/pdf");
      ServletOutputStream out = null;
      try {
          out = response.getOutputStream();
          out.write(document.getDocument());
          out.flush();
          FacesContext.getCurrentInstance().getResponseComplete();
          FacesContext.getCurrentInstance().renderResponse();
      } catch (IOException err) {
          err.printStackTrace();
      } finally {
          try {
              if (out != null) {
                  out.close();
              }
          } catch (IOException err) {
              err.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>This works, and perfectly. But I get an exception on my stack trace, and I would like to avoid having exception, because this isn't good behaviour. My exception is this :</p>
  
  <pre><code>2015-12-11T13:08:03.149+1100|Severe: Error Rendering View[/pages/private/merchant_statement/manageMerchantsDocuments.xhtml]
  java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.Response.getWriter(Response.java:777)
  at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:224)
  at com.sun.faces.context.ExternalContextImpl.getResponseOutputWriter(ExternalContextImpl.java:846)
  at com.sun.faces.application.view.FaceletViewHandlingStrategy.createResponseWriter(FaceletViewHandlingStrategy.java:1192)
  at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:403)
  at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
  at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
  at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
  at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
  at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
  at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
  at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
  at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
  at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
  at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>It is because somewhere in javax.faces.jar it calls the method getWriter(), which I cannot use because I used getOutputStream. </p>
  
  <p>And I am not sure about what I should do.</p>
  
  <p>Is there a way to download a file using getWriter() instead of using getOutputStream()? 
  And why does it need to render the view if I am staying on the same page?
  Is there a way to avoid my program to throw this exception?</p>
  
  <p>Any help would be appreciated, thanks !</p>