title: How to send PDF file data as a response using Servlet?
body: <p>My requirement is responding the PDF data to the mobile client(iPhone) using HTTP Servlet.</p>
  
  <p>I did in the following way, But I am not getting expected output in the client.</p>
  
  <pre><code>    PrintWriter out = response.getWriter();
  
      String aInputFileName = "/Users/hcl/Desktop/Easwar/sample.pdf";
          log("Reading in binary file named : " + aInputFileName);
          File file = new File(aInputFileName);
          log("File size: " + file.length());
          byte[] result = new byte[(int)file.length()];
          System.out.println("Length : "+  result.length);
          try {
            InputStream input = null;
            try {
              int totalBytesRead = 0;
              input = new BufferedInputStream(new FileInputStream(file));
              while(totalBytesRead &lt; result.length){
                int bytesRemaining = result.length - totalBytesRead;
                //input.read() returns -1, 0, or more :
                int bytesRead = input.read(result, totalBytesRead, bytesRemaining); 
                if (bytesRead &gt; 0){
                  totalBytesRead = totalBytesRead + bytesRead;
                }
              }
          response.setHeader("Content-Type", "application/pdf");
  
          out.println(result);
  </code></pre>
  
  <p>Is the method I am following right? Please advice. </p>
  
  <p>Thanks. </p>