title: URLConnection and content length : how much data is download?
body: <p>I've created a servlet which reads the content of a file to a byte array which subsequently is written to the OutputStream of the response:</p>
  
  <pre><code>// set headers
  resp.setHeader("Content-Disposition","attachment; filename=\"file.txt\"");
  resp.setHeader("Content-Length", "" + fileSize);
  
  // output file content.
  OutputStream out = resp.getOutputStream();
  out.write(fileBytes);
  out.close();
  </code></pre>
  
  <p>Now, I've also written a "client" which needs to find out how big the file is. This should be easy enough as I've added the "Content-Length" header.</p>
  
  <pre><code>URLConnection conn = url.openConnection();
  long fileSize = conn.getContentLength();
  </code></pre>
  
  <p>However, I am a little uncertain about the big picture. As I understand my own servlet, the entire file content is dumped to the OutputStream of the response. However, does calling getContentLength() also result in the actual file data somehow partially or fully being downloaded? In other words, when i invoke conn.getContentLength(), how much of the file will be returned from the server? Does the headers come "separate" from the content?</p>
  
  <p>All input highly appreciated!</p>