java 如何在浏览器中显示PDF文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32476706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display PDF file in browser
提问by vineeth
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();
采纳答案by aleks.lector
u can try to do the same with
你可以尝试做同样的事情
response.setHeader("Content-Disposition", "attachment;filename="+filepath+";");
回答by SaviNuclear
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());
回答by Golgot Remus
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" 。