简体   繁体   中英

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();

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());

你可以尝试做同样的事情

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

This is related

Is your browser Firefox? This might be relevant

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM