简体   繁体   English

如何在浏览器中显示PDF文件

[英]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. 在我的servlet中,我使用下面的代码在浏览器中打开PDF文件,但它显示了一个下载对话框。

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? 你的浏览器是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 ). 如果你想在选项卡中显示pdf,你需要在html(或angular,jsp,你使用的任何框架)中设置target =“_ blank”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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