我的 Web 应用程序生成一个 XML 文件。我正在使用 Struts2 流结果来管理下载,这是 struts.xml 中的操作:
<action name="generateXML" class="navigation.actions.GenerateXML">
<result type="stream">
<param name="contentType">text/xml</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
...
</action>
这是创建 FileInputStream“inputStream”的操作类“GenerateXML”的一部分:
public String execute() {
File xml = new File(filename);
...//fill the file with stuff
try {
setInputStream(new FileInputStream(xml));
} finally {
//inputStream.close();
xml.delete();
}
}
删除文件将不起作用,因为 inputStream 尚未关闭(该部分已被注释掉)。但是,如果我此时关闭它,则用户下载的 xml 文件为空,因为它的流在 struts 生成下载之前已关闭。除了使用定期删除服务器上的那些临时文件的脚本之外,还有没有办法在 struts 完成它之后关闭“inputStream”?