本文介绍了在新的浏览器选项卡中打开ResponseEntity PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我遇到了一个有用的PDF生成代码,用于在Spring MVC应用程序中向客户端显示该文件(返回生成PDF使用Spring MVC ):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"):@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;}我添加了声明该方法返回PDF文件(\"Spring 3.0 Java REST返回PDF文档): produce =application / pdf 。I added a declaration that the method returns a PDF file ("Spring 3.0 Java REST return PDF document"): produces = "application/pdf".我的问题是,当执行上面的代码时,它会立即要求客户端保存PDF文件。我希望首先在浏览器中查看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内容(从Spring MVC控制器方法提供)以显示在新窗口中建议添加 target =_ blank。我测试了它,正如预期的那样,它显示了一个新选项卡,但保存提示再次出现。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.另一个是我无法在我的浏览器中通过Java 的方法打开.pdf来添加 httpServletResponse.setHeader(Content-处置,内联); 但我不使用 HttpServletRequest 来提供我的PDF文件。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.如果给出我的代码/情况,如何在新选项卡中打开PDF文件?How can I open the PDF file in a new tab given my code/situation?推荐答案尝试httpServletResponse.setHeader("Content-Disposition", "inline");但是使用responseEntity如下。But using the responseEntity as follows.HttpHeaders headers = new HttpHeaders();headers.add("content-disposition", "attachment; filename=" + fileName)ResponseEntity<byte[]> response = new ResponseEntity<byte[]>( pdfContents, headers, HttpStatus.OK);应该有效不确定这个,但似乎你使用了不好的setContentDispositionFormData,尝试>Not sure about this, but it seems you are using bad the setContentDispositionFormData, try>headers.setContentDispositionFormData("attachment", fileName);请告知我是否有效 更新 此行为取决于您尝试服务的浏览器和文件。使用内联,浏览器将尝试在浏览器中打开文件。 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);或headers.add("content-disposition", "inline;filename=" + fileName)阅读本文以了解内容内联和附件之间 这篇关于在新的浏览器选项卡中打开ResponseEntity PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 20:20