繁体   English   中英

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

[英]How to display PDF file in browser

在我的servlet中,我使用下面的代码在浏览器中打开PDF文件,但它显示了一个下载对话框。

我做错了什么?

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

您必须使用以下配置设置响应类型: -

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+";");

是相关的

你的浏览器是Firefox吗? 可能是相关的

你需要这个:

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

否则,浏览器将提示您打开/保存。 (如果内容类型是八位字节流,或内容处置是附件)

如果你想在选项卡中显示pdf,你需要在html(或angular,jsp,你使用的任何框架)中设置target =“_ blank”。

暂无
暂无

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

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