gpt4 book ai didi

java - 如何让 PDF 内容(由 Spring MVC Controller 方法提供)显示在新窗口中

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:40 25 4
gpt4 key购买 nike

我是 Spring MVC 的新手,但它的功能给我留下了深刻的印象。

我正在使用 3.1.0-RELEASE,我必须显示 PDF 以响应表单:表单提交。

这是我在 Controller 中编写的(小)代码:

@RequestMapping(value = "new_product", method = RequestMethod.POST, params = "print")
@ResponseBody
public void saveAndShowPDF(ModelMap map, ShippingRequestInfo requestInfo, HttpServletRequest request, HttpServletResponse httpServletResponse) throws IOException {
saveProductChanges(map, requestInfo, request, httpServletResponse);
httpServletResponse.setContentType("application/pdf");
byte[] pdfImage = productService.getPDFImage(requestInfo.getRequestId());
httpServletResponse.getOutputStream().write(pdfImage);
}

此代码将 PDF byte[] 发送回原始窗口。

如何让 PDF 显示在单独的窗口中,以便我仍然可以在原始浏览器窗口中显示一些其他内容?最好的方法是使用客户端 PDF 查看程序(Adobe Reader、FoxIt 等)显示 PDF,但如果 PDF 显示在单独的浏览器窗口中,我会觉得没问题。

编辑:我决定设置 Content-Disposition,以便浏览器弹出一个保存/打开框,用户可以在其中打开 Adob​​e(丢失主浏览器页面)。

httpServletResponse.setHeader("Content-Disposition","attachment;filename=cool.pdf");

谢谢大家!

最佳答案

在提交表单的 form:form 标记中指定 target="_blank"

关于java - 如何让 PDF 内容(由 Spring MVC Controller 方法提供)显示在新窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com