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


