title: Write byte array to pdf file on click of view
body: <p>I am done with writing the file content to pdf file on click of view, But it's not opening automatically.Can any one help. Thanks in advance.And please correct me if anything wrong here.</p>
  
  <pre><code>@RequestMapping(value = { "/view" }, method = RequestMethod.GET)
          public static byte[] loadFile(@RequestParam("file_path") String file_path,@RequestParam("file_name") String fileName,HttpServletResponse response, HttpServletRequest request,HttpServletResponse resp) throws IOException, DocumentException {
  
          FileInputStream inputStream = null;
          try {
              String filePath = file_path.replace("/", "\\");
              File file = new File(filePath);
              inputStream = new FileInputStream(file);
              filePath.endsWith(".pdf");
              return readFully(inputStream, filePath, fileName, resp,   request,
                      resp);
          } finally {
              if (inputStream != null) {
                  inputStream.close();
              }
          }
      }
  </code></pre>
  
  <p>here i am calling the below method and written the code for opening a file automatically. </p>
  
  <pre><code>@ResponseBody
      public static byte[] readFully(FileInputStream inputStream,
              @RequestParam("file_path") String file_path,
              @RequestParam("file_name") String fileName,
              HttpServletResponse response, HttpServletRequest request,
              HttpServletResponse resp) throws IOException, DocumentException {
          byte[] buffer = new byte[8192];
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          int bytesRead;
          while ((bytesRead = inputStream.read(buffer)) != -1) {
              baos.write(buffer, 0, bytesRead);
          }
          Document doc = new Document();
          PdfWriter.getInstance(doc, baos);
          doc.open();
          response.setContentType("application/pdf");
          response.setHeader("Content-disposition", "attachment; filename="
                  + fileName + ".pdf");
          response.setHeader("Cache-Control",
                  "must-revalidate, post-check=0, pre-check=0");
          response.setHeader("Expires", "0");
          response.setHeader("Pragma", "No-cache");
          response.setContentLength(baos.size());
          ServletOutputStream outn = response.getOutputStream();
          outn.flush();
          baos.writeTo(outn);
          File f = new File("C:\\Users\\Downloads\\" + fileName + ".pdf");
          if (f.exists()) {
  
              if (Desktop.isDesktopSupported()) {
                  Desktop.getDesktop().open(f);
              } else {
                  System.out.println("Awt Desktop is not supported!");
              }
  
          } else {
              System.out.println("File is not exists!");
          }
          System.out.println("Done");
          return baos.toByteArray();
      }   
  </code></pre>