title: JSP download - application/octet-stream
body: <p>I have a page in JSP that list some file that could be downloaded by a user.
  Thoses files are not on the local server, they are on a remote file server.</p>
  
  <p>When the user click to download a file, the webserver connect via TCP to the file server. The web server download the file and create a HTTP response for the client. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;%@page language="java"%&gt;
  &lt;%@page import="sun.misc.Request"%&gt;
  &lt;%@page import="listing.ClientTCPStockage"%&gt;
  &lt;%@page import="java.net.InetAddress"%&gt;
  
  &lt;%
  out.clearBuffer();
  
  String nomFichier = request.getParameter("fichier");
  String adresseStockage = request.getParameter("adresseStockage");
  
  ClientTCPStockage clientStockage = new ClientTCPStockage(InetAddress.getByName(adresseStockage), 2004);
  byte donneeFichier[] = clientStockage.getDonneesFichier(nomFichier);
  
  response.setHeader("Content-Disposition", "attachment;filename=\"" + nomFichier + "\"");
  response.setHeader("Content-Type", "application/octet-stream;");
  response.setHeader("Accept-Ranges", "bytes");
  response.setHeader("Content-Length", String.valueOf(donneeFichier.length));
  
  for(int i = 0; i &lt; donneeFichier.length; i++){
      out.write(donneeFichier[i]);
  }
  %&gt;
  </code></pre>
  
  <p>This is working perfectly fine for text-based file, like .csv or normal .txt 
  but It doesnt work for other type like .mp3 or .jpeg.. the files end up corrupt.</p>
  
  <p>I think there is a problem with my encoding but I can't find where..</p>
  
  <p>Here is the HTTP Header response: </p>
  
  <pre><code>HTTP/1.x 200 OK
  Server: Apache-Coyote/1.1
  Content-Disposition: attachment;filename="test.mp3"
  Accept-Ranges: bytes
  Content-Type: application/octet-stream;
  Content-Length: 5387668
  Date: Sun, 20 Dec 2009 18:52:18 GMT
  </code></pre>
  
  <p>Thanks.</p>