java 关闭时是否有现有的 FileInputStream 删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4693968/
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
is there an existing FileInputStream delete on close?
提问by Shawn D.
Is there an existing way to have a FileInputStream
delete the underlying file automatically when closed?
是否有现有的方法可以FileInputStream
在关闭时自动删除基础文件?
I was planning to make my own utility class to extend FileInputStream
and do it myself, but I'm kinda surprised that there isn't something already existing.
我打算制作我自己的实用程序类来扩展FileInputStream
和自己做,但我有点惊讶还没有现有的东西。
edit:Use case is that I have a Struts 2 action that returns an InputStream
for file download from a page. As far as I can tell, I don't get notified when the action is finished, or the FileInputStream
is not in use anymore, and I don't want the (potentially large) temporary files that are generated to be downloaded left lying around.
编辑:用例是我有一个 Struts 2 操作,它InputStream
从页面返回一个for 文件下载。据我所知,当操作完成或FileInputStream
不再使用时,我不会收到通知,并且我不希望生成的(可能很大)临时文件被下载。
The question wasn't Struts 2 specific, so I didn't include that info originally and complicate the question.
这个问题不是特定于 Struts 2 的,所以我最初没有包含该信息并使问题复杂化。
回答by nos
There's no such thing in the standard libraries, and not any of the apache-commons libs either , so something like:
标准库中没有这样的东西,也没有任何 apache-commons 库,所以像:
public class DeleteOnCloseFileInputStream extends FileInputStream {
private File file;
public DeleteOnCloseFileInputStream(String fileName) throws FileNotFoundException{
this(new File(fileName));
}
public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException{
super(file);
this.file = file;
}
public void close() throws IOException {
try {
super.close();
} finally {
if(file != null) {
file.delete();
file = null;
}
}
}
}
回答by user2044089
I know this is a fairly old question; however, it's one of the first results in Google, and Java 7+ has this functionality built in:
我知道这是一个相当古老的问题;但是,它是 Google 中的第一个结果,Java 7+ 内置了此功能:
Path path = Paths.get(filePath);
InputStream fileStream = Files.newInputStream(path, StandardOpenOption.DELETE_ON_CLOSE);
There are a couple caveats with this approach though, they're written up here, but the gist is that the implementation makes a best effort attempt to delete the file when the input stream is closed, and if that fails makes another best effort attempt when the JVM terminates. It is intended for use with temp files that are used solely by a single instance of the JVM. If the application is security sensitive, there are also a few other caveats.
虽然这种方法有几个警告,它们写在这里,但要点是实现尽最大努力尝试在输入流关闭时删除文件,如果失败,则再次尽力尝试JVM 终止。它旨在与仅由 JVM 的单个实例使用的临时文件一起使用。如果应用程序是安全敏感的,还有一些其他注意事项。
回答by Pierre
Can you can use File.deleteOnExit()before opening the file ?
您可以在打开文件之前使用File.deleteOnExit()吗?
EDIT: On you can subclass a FileInputStream that will delete the file on 'close()';
编辑:在您可以子类化 FileInputStream 时,它将删除“close()”上的文件;
class MyFileInputStream extends FileInputStream
{
File file;
MyFileInputStream(File file) { super(file); this.file=file;}
public void close() { super.close(); file.delete();}
}
回答by Conrad Herrmann
I know this is an old question, but I just ran into this issue, and found another answer: javax.ws.rs.core.StreamingOutput.
我知道这是一个老问题,但我刚刚遇到了这个问题,并找到了另一个答案:javax.ws.rs.core.StreamingOutput。
Here's how I used it:
这是我如何使用它:
File downloadFile = ...figure out what file to download...
StreamingOutput so = new StreamingOutput(){
public void write(OutputStream os) throws IOException {
FileUtils.copyFile(downloadFile, os);
downloadFile.delete();
}
ResponseBuilder response = Response.ok(so, mimeType);
response.header("Content-Disposition", "attachment; filename=\""+downloadFile.getName()+"\"");
result = response.build();