Java 在新的浏览器选项卡中打开 ResponseEntity PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21870802/
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
Open ResponseEntity PDF in new browser tab
提问by
I came across a helpful PDF generation code to show the file to the client in a Spring MVC application ("Return generated PDF using Spring MVC"):
我遇到了一个有用的 PDF 生成代码,可以在 Spring MVC 应用程序中向客户端显示文件(“使用 Spring MVC 返回生成的 PDF”):
@RequestMapping(value = "/form/pdf", produces = "application/pdf")
public ResponseEntity<byte[]> showPdf(DomainModel domain, ModelMap model) {
createPdf(domain, model);
Path path = Paths.get(PATH_FILE);
byte[] pdfContents = null;
try {
pdfContents = Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = NAME_PDF;
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(
pdfContents, headers, HttpStatus.OK);
return response;
}
I added a declaration that the method returns a PDF file ("Spring 3.0 Java REST return PDF document"): produces = "application/pdf"
.
我添加了一个声明,表明该方法返回一个PDF文件(“春3.0的Java REST回报PDF文档”): produces = "application/pdf"
。
My problem is that when the code above is executed, it immediately asks the client to save the PDF file. I want the PDF file to be viewed first in the browser so that the client can decide whether to save it or not.
我的问题是,当上面的代码执行时,它立即要求客户端保存PDF文件。我希望首先在浏览器中查看 PDF 文件,以便客户端可以决定是否保存它。
I found "How to get PDF content (served from a Spring MVC controller method) to appear in a new window"that suggests to add target="_blank"
in the Spring form tag. I tested it and as expected, it showed a new tab but the save prompt appeared again.
我发现“如何获取 PDF 内容(由 Spring MVC 控制器方法提供)出现在新窗口中”,建议添加target="_blank"
Spring 表单标签。我测试了它,正如预期的那样,它显示了一个新选项卡,但再次出现了保存提示。
Another is "I can't open a .pdf in my browser by Java"'s method to add httpServletResponse.setHeader("Content-Disposition", "inline");
but I don't use HttpServletRequest
to serve my PDF file.
另一个是“我无法通过 Java 在浏览器中打开 .pdf”的添加方法,httpServletResponse.setHeader("Content-Disposition", "inline");
但我不用于HttpServletRequest
提供我的 PDF 文件。
How can I open the PDF file in a new tab given my code/situation?
给定我的代码/情况,如何在新选项卡中打开 PDF 文件?
采纳答案by Koitoer
Try
尝试
httpServletResponse.setHeader("Content-Disposition", "inline");
But using the responseEntity as follows.
但是使用 responseEntity 如下。
HttpHeaders headers = new HttpHeaders();
headers.add("content-disposition", "attachment; filename=" + fileName)
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(
pdfContents, headers, HttpStatus.OK);
It should work
它应该工作
Not sure about this, but it seems you are using bad the setContentDispositionFormData, try>
对此不确定,但您似乎使用了错误的 setContentDispositionFormData,请尝试>
headers.setContentDispositionFormData("attachment", fileName);
Let me know if that works
让我知道这是否有效
UPDATE
更新
This behavior depends on the browser and the file you are trying to serve. With inline, the browser will try to open the file within the browser.
此行为取决于浏览器和您尝试提供的文件。使用内联,浏览器将尝试在浏览器中打开文件。
headers.setContentDispositionFormData("inline", fileName);
Or
或者
headers.add("content-disposition", "inline;filename=" + fileName)
Read this to know difference between inline and attachment
阅读本文以了解内联和附件之间的区别
回答by mykey
/* Here is a simple code that worked just fine to open pdf(byte stream) file
* in browser , Assuming you have a a method yourService.getPdfContent() that
* returns the bite stream for the pdf file
*/
@GET
@Path("/download/")
@Produces("application/pdf")
public byte[] getDownload() {
byte[] pdfContents = yourService.getPdfContent();
return pdfContents;
}
回答by Danilo Teodoro
What happened is that since you "manually" provided headers to the response, Spring did not added the other headers (e.g. produces="application/pdf"). Here's the minimum code to display the pdf inline in the browser using Spring:
发生的事情是,由于您“手动”为响应提供了标头,因此 Spring 没有添加其他标头(例如,produce="application/pdf")。这是使用 Spring 在浏览器中内联显示 pdf 的最少代码:
@GetMapping(value = "/form/pdf", produces = "application/pdf")
public ResponseEntity<byte[]> showPdf() {
// getPdfBytes() simply returns: byte[]
return ResponseEntity.ok(getPdfBytes());
}