关闭 struts 使用的 FileInputStream流结果
我的 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”?
My web application generates an XML file. I'm using a Struts2 stream result to manage the download, here's the action in 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>
Here's the part of the action class "GenerateXML" where the FileInputStream "inputStream" is created:
public String execute() {
File xml = new File(filename);
...//fill the file with stuff
try {
setInputStream(new FileInputStream(xml));
} finally {
//inputStream.close();
xml.delete();
}
}
Deleting the file won't work because the inputStream isn't closed yet (That part is commented out). However, if i close it at this point the xml file downloaded by the user is empty since its stream was closed before struts generates the download.
Besides using a script that regularly deletes those temporary files on the server, is there a way to close "inputStream" AFTER struts has done its thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关闭输入流上没有删除,但您可以编写自己的。请参阅关闭时是否删除现有 FileInputStream?。
这个想法是,您不传递 FileInputStream,而是传递 ClosingFileInputStream,它会覆盖 close 并在调用 close 时删除文件。 close() 将由 struts 调用:
有关更多信息,请参阅链接的问题。
There isn't a delete on close input stream, but you can write your own. See is there an existing FileInputStream delete on close?.
The idea is that you don't pass a FileInputStream, but pass your ClosingFileInputStream, which overrides close and deletes the file when close is called. The close() will be called by struts:
See the linked question for more information.
你不需要那样做。
Struts2 会注意关闭 Steam 本身,您所需要做的就是创建一个输入流并设置它。
以下是 struts2 如何为您处理流关闭
因此,由于流是 struts2 中的结果类型,它所做的就是从您定义的流中选取数据,刷新它并关闭它。
我希望它能消除你的疑虑。
You need not do that.
Struts2 will take care to close the steam itself all you need to do is to create a input stream and set it.
Here is how struts2 handle stream closing for you
So since stream is a result type in struts2 what it doing is that it picking the data from the stream you have defined flushing it and den closing it.
i hope it will clear your doubt.