title: Stream linearized PDF from servlet to browser (fast web view)
body: <p>I'm running a web app that provides a servlet. this servlet opens a pdf file from a Network File System and finally streams it to the requesting browser.</p>
  
  <p>All the pdf files are linearized by adobe lifecycle pdf generator and ready for fast web view.</p>
  
  <p>unfortunately, the fast web view does not work. I guess it's a problem of how to open and stream the file in java code and the setting of response header info.
  if i deploy a test pdf within my webapp onto the jboss AS and open it directly from the browser by url, the incrementel loading works.</p>
  
  <p>can anyone help me?</p>
  
  <p>Here's the code of my servlet:</p>
  
  <pre><code>response.setContentType("application/pdf");
  response.setHeader("Expires", "0");
  response.setHeader("Cache-Control",
      "must-revalidate, post-check=0, pre-check=0");
  response.setHeader("Content-Disposition",
      "inline;filename=" + documentReference);
  response.setHeader("Accept-Ranges", "bytes");
  
  File nfsPDF = new File(NFS_DIRECTORY_PATH + documentReference);
  
  FileInputStream fis = new FileInputStream(nfsPDF);
  BufferedInputStream bis = new BufferedInputStream(fis);
  ServletOutputStream sos = response.getOutputStream();
  byte[] buffer = new byte[(int) nfsPDF.length()];
  while (true) {
     int bytesRead = bis.read(buffer, 0, buffer.length);
     if (bytesRead &lt; 0) {
        break;
     }
     sos.write(buffer, 0, bytesRead);
  }
  sos.flush();
  //... closing...
  </code></pre>