title: How to send a file from a servlet the "right" way?
body: <p>I'm trying to send a file to a user from an http servlet.
  The servlet runs some identification tests (on the request) and then sends the client a file.</p>
  
  <p>This generally works but now I turned on my TOMCAT server redirects to https and when I try to access the servlet and download the file from either IE6 or IE8 it fails and I get this exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot forward after response has been committed
  </code></pre>
  
  <p>(on the localhost.log)</p>
  
  <p>and </p>
  
  <pre><code>ClientAbortException:  java.net.SocketException: Connection reset by peer: socket write error
  </code></pre>
  
  <p>(in the servlet log)</p>
  
  <p>The code that does the sending (Simplified):</p>
  
  <pre><code>private void sendFile(HttpServletResponse response, byte[] file, String clientFileName)
  {
      ServletOutputStream op = null;
  
      setContentType(response);
      response.setHeader("Content-Disposition", "attachment; filename=\"" + clientFileName + "\"");
      // send byte array to output buffer. 
      op = response.getOutputStream();
  
      // Content Length must be set after all processing done.
      response.setContentLength((int) file.length);   
      op.write(file);
  }
  </code></pre>
  
  <p>Is this the correct way to send a file from a servlet? What's the best way?</p>
  
  <p>Thanks!!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Used the code from @BalusC artice in this link:
  <a href="http://balusc.blogspot.com/2007/07/fileservlet.html" rel="nofollow">http://balusc.blogspot.com/2007/07/fileservlet.html</a></p>
  
  <p>This made it work.</p>
  
  <p>Still doesn't work in IE6-IE8 when used from Gmail because of a filtering stage gmail adds to downloads in these browsers.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>The problem seems to be with Gmail + Internet Explorer 6-8. 
  I'm Assuming that gmail is doing a redirect (That's actually pretty obvious if you look at the url on the page after you click the link in the mail).
  Is Client-Pull technique my only solution?</p>