vineeth Prabhakaran vineeth Prabhakaran - 2 months ago 24
Java Question

How to display PDF file in browser

In my servlet I am using the code below to open a PDF file in a browser, but instead, it shows a download dialog box.

What I am doing wrong?

response.setContentType("application/pdf");
out = response.getWriter();
String filepath = "D:/MyFolder/PDF/MyFile.pdf";

response.setHeader("Content-Disposition", "inline; filename=" + filepath + ";");
FileOutputStream fileOut = new FileOutputStream("D:/MyFolder/PDF/MyFile.pdf");

fileOut.close();
out.close();

Answer

u can try to do the same with

response.setHeader("Content-Disposition", "attachment; ilename="+filepath+";");