栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在有限的时间内将PDF存储在应用服务器上,并使其可供下载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在有限的时间内将PDF存储在应用服务器上,并使其可供下载

利用

File#createTempFile()
设施。servletcontainer管理的临时文件夹可作为具有
ServletContext.TEMPDIR
键的应用程序范围属性使用。

String tempDir = (String) externalContext.getApplicationMap().get(ServletContext.TEMPDIR);File tempPdfFile = File.createTempFile("generated-", ".pdf", tempDir);// Write to it.

然后将自动生成的文件名传递给负责服务的文件名。例如

String tempPdfFileName = tempPdfFile.getName();// ...

最后,一旦使用文件名作为参数调用负责提供服务的对象,则只需按如下所示对其进行流处理:

String tempDir = (String) getServletContext().getAttribute(ServletContext.TEMPDIR);File tempPdfFile = new File(tempDir, tempPdfFileName);response.setHeader("Content-Type", "application/pdf");response.setHeader("Content-Length", String.valueOf(tempPdfFile.length()));response.setHeader("Content-Disposition", "inline; filename="generated.pdf"");Files.copy(tempPdfFile.toPath(), response.getOutputStream());


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379338.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号