6

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();
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
vineeth
  • 641
  • 4
  • 11
  • 25

4 Answers4

7

As you have to set the response type with the following configuration:-

File outPutFile=new File(generatedFile);
stream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
response.setContentLength((int) outPutFile.length());
SaviNuclear
  • 886
  • 1
  • 7
  • 19
2

you need this:

response.setContentType("application/pdf") 
response.setHeader("Content-Disposition", "inline; filename= .. " )

Otherwise, the browser will prompt you to open/save. ( if content type is octet-stream, or content-disposition is attachment )

if you want the pdf to be displayed in a tab, you need to set target = "_blank" in the html ( or angular, jsp, whatever framework you are using ).

Golgot Remus
  • 86
  • 1
  • 3
1

u can try to do the same with

response.setHeader("Content-Disposition", "attachment;filename="+filepath+";");
wittakarn
  • 3,124
  • 1
  • 18
  • 31
0

This is related

Is your browser Firefox? This might be relevant