关闭 struts 使用的 FileInputStream流结果

发布于 2024-12-18 00:33:01 字数 914 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2024-12-25 00:33:02

关闭输入流上没有删除,但您可以编写自己的。请参阅关闭时是否删除现有 FileInputStream?

这个想法是,您不传递 FileInputStream,而是传递 ClosingFileInputStream,它会覆盖 close 并在调用 close 时删除文件。 close() 将由 struts 调用:

public String execute() {    
    File xml = new File(filename);
        ...//fill the file with stuff
        setInputStream(new ClosingFileInputStream(xml));   
    }

有关更多信息,请参阅链接的问题。

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:

public String execute() {    
    File xml = new File(filename);
        ...//fill the file with stuff
        setInputStream(new ClosingFileInputStream(xml));   
    }

See the linked question for more information.

方觉久 2024-12-25 00:33:02

你不需要那样做。
Struts2 会注意关闭 Steam 本身,您所需要做的就是创建一个输入流并设置它。

以下是 struts2 如何为您处理流关闭

public class StreamResult extends StrutsResultSupport {
  // removing all other code
      {
        // Flush
                oOutput.flush();
                }
                   finally {
                if (inputStream != null) inputStream.close();
                  if (oOutput != null) oOutput.close();
               }

    }

因此,由于流是 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

public class StreamResult extends StrutsResultSupport {
  // removing all other code
      {
        // Flush
                oOutput.flush();
                }
                   finally {
                if (inputStream != null) inputStream.close();
                  if (oOutput != null) oOutput.close();
               }

    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文