title: How to set content length for file downloaded using URL class, using Java/Spring MVC?
body: <p>Following is Java code used to download file in Spring MVC Ajax call. 
  I need to set Content Length which I am unable to find using URL.openStream().</p>
  
  <pre><code>URL fileToDownload = new URL(form.getDownloadLink());
  
  InputStream fileContents = fileToDownload.openStream();
  response.setContentType("application/docx");
  response.setHeader("Content-Disposition", "attachment; filename="+fileName+"");
  response.setContentLength(&lt;Length&gt;);
  
  ServletOutputStream outputFile = response.getOutputStream(); 
  IOUtils.copy(fileContents, outputFile);
  response.flushBuffer();
  fileContents.close();
  outputFile.close();
  </code></pre>
  
  <p>Could anyone guide how to get length of Stream? </p>
  
  <p>We download PDF and DOCX files of different sizes using above</p>