我有一个为客户端生成 docx 文件的按钮。我遇到的问题是生成的文件交给客户端后无法删除。服务器端技术是 JSF (XPages)。该按钮调用以下代码:
var v = new ru.generate.doc.generateDoc();
v.generateReport2();
该generateReport2()
方法调用 2 个重要方法generateFromTemplateAndReturnPath(String template)
:
private String generateFromTemplateAndReturnPath(String pathToTemplate)
{
XWPFDocument document = new XWPFDocument(OPCPackage.open(pathToTemplate));
String pathToFinalResult = FilenameUtils.getFullPath(pathToTemplate) + "reportTest.docx";
FileOutputStream fous = new FileOutputStream(pathToFinalResult);
document.write(fous);
fous.close();
return pathToFinalResult;
}
以及将giveDocumentForClient(String pathToCompleteFile)
报告提供给客户的方法:
private void giveDocumentForClient(String pathToCompleteFile) throws InvalidFormatException, IOException
{
String fn = new File(pathToCompleteFile).getName();
XspHttpServletResponse response = (XspHttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setContentType(URLConnection.guessContentTypeFromName(fn));
response.setHeader("Content-disposition", "attachment; filename=" + fn);
OutputStream output = response.getOutputStream();
output.write(IOUtils.toByteArray(new FileInputStream(new File(pathToCompleteFile))));
output.close();
FacesContext.getCurrentInstance().responseComplete();
}
完成此方法后,我尝试在内部进行此操作generateReport2()
:
new File(pathToCompleteFile).delete()
无论我做什么,这个方法都会返回 false。我也试过:
FileDeleteStrategy.FORCE.delete(new File(pathToSubstitutedDoc))
但还是没有结果。
canRead()、canWrite()、canExecute() 等方法总是返回 true。SecurityManager 也毫无例外地执行方法。问题是什么?