-2

I am new to javascript and i have a requirement where i need to open pdf file on click of submit button which is located on server. Also i need to redirect it to new jsp. I tried using window.open but it did not open as it is not able to fetch file from server location. a href is also not working. can you people suggest me.

Diya
  • 3
  • 1

1 Answers1

0

There are different ways to do it, if you are using servlets you can write the right MIME type into the response and write the PDF to the servlet stream

Something like:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/pdf;charset=UTF-8");

    response.addHeader("Content-Disposition", "inline; filename=" + "cities.pdf");
    ServletOutputStream out = response.getOutputStream();

    InputStream inputStream = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {

        inputStream = new FileInputStream("sourcePath_of_pdf.pdf");

        byte[] buffer = new byte[1024];
        baos = new ByteArrayOutputStream();

        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, bytesRead);
        }

    } catch (FileNotFoundException e) {
            e.printStackTrace();
    }

    baos.writeTo(out);
}
Rafael
  • 104
  • 8