java Tomcat 6:如何在 Web 方法调用结束后删除临时文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/158568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat 6: how to delete temporary files after a web method call has ended?
提问by Jen A
I have a temporary file with data that's returned as part of a SOAP response via a MTOM binary attachment. I would like to trash it as soon as the method call "ends" (i.e., finishes transferring). What's the best way for me to do this? The best way I can figure out how to do this is to delete them when the session is destroyed, but I'm not sure if there's a more 'immediate' way to do this.
我有一个临时文件,其中包含通过 MTOM 二进制附件作为 SOAP 响应的一部分返回的数据。我想在方法调用“结束”(即完成传输)后立即将其丢弃。我这样做的最佳方法是什么?我能弄清楚如何做到这一点的最好方法是在会话被破坏时删除它们,但我不确定是否有更“立即”的方法来做到这一点。
FYI, I'm NOT using Axis, I'm using jax-ws, if that matters.
仅供参考,我没有使用 Axis,我使用的是 jax-ws,如果这很重要的话。
UPDATE: I'm not sure the answerers are really understanding the issue. I know how to delete a file in java. My problem is this:
更新:我不确定回答者是否真的理解这个问题。我知道如何在java中删除文件。我的问题是这样的:
@javax.jws.WebService
public class MyWebService {
...
@javax.jws.WebMethod
public MyFileResult getSomeObject() {
File mytempfile = new File("tempfile.txt");
MyFileResult result = new MyFileResult();
result.setFile(mytempfile); // sets mytempfile as MTOM attachment
// mytempfile.delete() iS WRONG
// can't delete mytempfile because it hasn't been returned to the web service client
// yet. So how do I remove it?
return result;
}
}
回答by Chris Dail
I ran into this same problem. The issue is that the JAX-WS stack manages the file. It is not possible to determine in your code when JAX-WS is done with the file so you do not know when to delete it.
我遇到了同样的问题。问题在于 JAX-WS 堆栈管理该文件。无法在您的代码中确定 JAX-WS 何时对文件完成,因此您不知道何时删除它。
In my case, I am using a DataHandler on my object model rather than a file. MyFileResult would have the following field instead of a file field:
就我而言,我在对象模型上使用 DataHandler 而不是文件。MyFileResult 将具有以下字段而不是文件字段:
private DataHandler handler;
My solution was to create a customized version of FileDataSource. Instead of returning a FileInputStream to read the contents of the file, I return the following extension of FileInputStream:
我的解决方案是创建一个自定义版本的 FileDataSource。我没有返回 FileInputStream 来读取文件的内容,而是返回 FileInputStream 的以下扩展名:
private class TemporaryFileInputStream extends FileInputStream {
public TemporaryFileInputStream(File file) throws FileNotFoundException {
super(file);
}
@Override
public void close() throws IOException {
super.close();
file.delete();
}
}
Essentially the datasource allows reading only once. After the stream is closed, the file is deleted. Since the JAX-WS stack only reads the file once, it works.
本质上,数据源只允许读取一次。流关闭后,文件将被删除。由于 JAX-WS 堆栈只读取文件一次,因此它可以工作。
The solution is a bit of a hack but seems to be the best option in this case.
该解决方案有点麻烦,但在这种情况下似乎是最佳选择。
回答by Steven M. Cherry
Are you using standard java temp files? If so, you can do this:
您使用的是标准的 Java 临时文件吗?如果是这样,您可以这样做:
File script = File.createTempFile("temp", ".tmp", new File("./"));
... use the file ...
script.delete(); // delete when done.
回答by anjanb
the work folder that you set up in the context for this webapp that you're talking about. Can you set this work directory in a known directory ? If yes, then you can find the temp file within the temp work directory(that you know). Once you find, you can delete it.
您在上下文中为您正在谈论的这个 web 应用程序设置的工作文件夹。你能在一个已知目录中设置这个工作目录吗?如果是,那么您可以在临时工作目录(您知道)中找到临时文件。一旦找到,您可以将其删除。

