title: Unable to download file using InputStream
body: <p>I currently use the following code to enable the user to download a file from an inputStream. I'm using Liferay 6.1 GA2 with JSF 2.0.</p>
  
  <p>The file in question is an image which is only 1.33kb and I have no problems opening it from the server. It also displays fine in the xhtml page. However, when it is downloaded to the client computer using this code, it ends up as a 36.7 kb file. This downloaded file then can't be opened.</p>
  
  <p>Even with an image of file size 558KB, the resulting downloaded file is also only 36.7kb.</p>
  
  <pre><code>    FacesContext facesContext = FacesContext.getCurrentInstance();
      OutputStream outputStream = null;
  
      try{
          ExternalContext externalContext = facesContext.getExternalContext();
          ActionResponse actionResponse = (ActionResponse) externalContext.getResponse();
          HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
  
          outputStream = externalContext.getResponseOutputStream();
  
          //Set the information needed in the response
          String contentType = MimeTypesUtil.getContentType(fileName);
          response.reset(); 
          response.setBufferSize(BUFFER_SIZE);
          externalContext.setResponseBufferSize(BUFFER_SIZE);
          response.setContentType(contentType); 
          response.setContentLength((int)fileSize);
          response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
  
          InputStream inputStream = getImageInputStream();
  
          int read = 0;
          byte[] bytes = new byte[BUFFER_SIZE];
  
          int count = 0;
          while ((read = inputStream.read(bytes)) != -1) {
            count++;
            outputStream.write(bytes, 0, read);
          }
  
          //The following always outputs the right size
          System.out.println("Wrote " + count + "x" + BUFFER_SIZE + "b");
  
      } catch (Exception ex){
          ex.printStackTrace();
      } finally {
          if (outputStream != null)
              IOUtils.closeQuietly(outputStream);
          facesContext.responseComplete();
      }
  </code></pre>
  
  <p>Does anyone have any idea what could be going wrong with this code?</p>
  
  <p><strong>Other codes tried</strong></p>
  
  <p>I have also tried using this code to set the response data. The page refreshes itself, the method gets called, but no file is downloaded.</p>
  
  <pre><code>    String contentType = MimeTypesUtil.getContentType(fileName);
      externalContext.responseReset();
      externalContext.setResponseBufferSize(BUFFER_SIZE);response.setContentType(contentType);
      externalContext.setResponseContentLength((int)fileSize);
      externalContext.setResponseHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
  </code></pre>
  
  <p>When I get the outputStream is the following manner:</p>
  
  <pre><code>    outputStream = response.getOutputStream(); 
  </code></pre>
  
  <p>I get the error saying that it's unable to get the Writer because the OutputStream is in use:</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
      com.liferay.portal.kernel.servlet.StringServletResponse.getWriter(StringServletResponse.java:102)
      org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
      org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:221)
      org.apache.jsp.html.common.themes.portal_jsp._jspService(portal_jsp.java:297)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
      ...
  </code></pre>
  
  <p>Any help or push in the right direction will be greatly appreciated!</p>