在有限时间内在应用服务器上存储PDF,并使其可供下载 [英] Store PDF for a limited time on app server and make it available for download

查看:139
本文介绍了在有限时间内在应用服务器上存储PDF,并使其可供下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里,我正在使用PrimeFaces 5 / JSF 2和tomcat!



有人可以告诉我,或者给我一个关于如何存储pdf的有限的想法时间在应用程序服务器(我正在使用tomcat),然后下载它(如果这是用户请求)。此功能与发票相关,因此我无法使用dataExporter。



更具体地说,我几乎实现了这一点,但我不太清楚。一个大问题是...我在哪里存储我生成的文件?我已经浏览过,人们说,将文件保存在webApp或tomcat目录中是不行的。我有什么其他solutiuon?

解决方案

使用 文件#createTempFile() 设施。 servlet容器管理的临时文件夹可作为应用程序作用域属性使用 ServletContext.TEMPDIR 作为关键。

  String tempDir =(String)externalContext.getApplicationMap()。get(ServletContext.TEMPDIR); 
文件tempPdfFile = File.createTempFile(generated-,.pdf,tempDir);
//写给它

然后只需将自动生成的文件名传递给负责管理的文件名。例如,

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

最后,一旦负责服务的人被调用文件名作为参数,例如一个简单的servlet ,然后按如下方式发送:

  String tempDir =(String)getServletContext ().getAttribute(ServletContext.TEMPDIR); 
文件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());



另请参见:




Hei there, I'm using PrimeFaces 5/JSF 2 and tomcat!

Can someone show me or give me an idea on how to store pdfs for a limited time on an application server(I'm using tomcat) and then download it (if that's what the user requests). This functionality relates to invoices so I can't use the dataExporter.

To be more specific, I pretty much implemented this but I don't feel so sure about it. One big question is... where do I store my generated files? I've browsed around and people said that it's not ok to save the files in the webApp or in the tomcat directory. What other solutiuon do I have?

解决方案

Make use of File#createTempFile() facility. The servletcontainer-managed temporary folder is available as application scoped attribute with ServletContext.TEMPDIR as key.

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

Then just pass the autogenerated file name around to the one responsible for serving it. E.g.

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

Finally, once the one responsible for serving it is called with the file name as parameter, for example a simple servlet, then just stream it as follows:

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());

See also:

这篇关于在有限时间内在应用服务器上存储PDF,并使其可供下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆