title: Show content of File
body: <p>i would like to open/show the file content in a browser but i dosen't work.
  This is my code:</p>
  
  <pre><code> @WebServlet("/Download")
  public class Download extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      String name = request.getParameter("id");
      String sep = File.separator;
      File file = new File("C:" + sep + "FILE" + sep + name);
  
      if(!file.exists())
      {
          response.getWriter().print("File not found");
          return;
      }
  
  
      InputStream in = new FileInputStream(file);
      byte[] buffer = new byte[4096];
      int i = 0;
      while((i = in.read(buffer)) != -1)
      { 
          baos.write(buffer, 0, i);
  
      }
  
  
       response.setContentType("application/pdf");
       response.setContentLength(baos.size());
       response.setHeader("Content-Disposition", "inline; filename=help.pdf");
       response.setHeader("Cache-Control", "cache, must-revalidate");
       response.setHeader("Pragma", "public");
  
      BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
  
      in.close();
  
      baos.writeTo(bos);
      baos.flush();
      bos.flush(); 
      bos.close();
  
  
    }
  
  }
  </code></pre>
  
  <p>I try to open that file with a servlet class.
  I browser debugger i can see that response is coming in, usually in this format(JVBERi0xLjMNMSAwIG9iag08PC9UeXBl...).</p>
  
  <p>Thanks for helping me</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>on the jsp site i call the servlet with an AJAX call, looks like this where 'id' is the fileName which the user want to open.</p>
  
  <pre><code>function downloadFile(id) {
  
  $.ajax({
      url:"Download",
      type:"POST",
      data:"id="+id       
  });
  </code></pre>
  
  <p>}</p>