is there an existing FileInputStream delete on close?

淺唱寂寞╮ 提交于 2019-12-18 03:05:11

问题


Is there an existing way to have a FileInputStream delete the underlying file automatically when closed?

I was planning to make my own utility class to extend FileInputStreamand do it myself, but I'm kinda surprised that there isn't something already existing.

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.

The question wasn't Struts 2 specific, so I didn't include that info originally and complicate the question.


回答1:


There's no such thing in the standard libraries, and not any of the apache-commons libs either , so something like:

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;
         }
       }
   }
}



回答2:


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:

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.




回答3:


Can you can use File.deleteOnExit() before opening the file ?

EDIT: On you can subclass a FileInputStream that will delete the file on 'close()';

class MyFileInputStream extends FileInputStream
{
File file;
MyFileInputStream(File file) { super(file); this.file=file;}
public void close() { super.close(); file.delete();}
}



回答4:


I know this is an old question, but I just ran into this issue, and found another answer: 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();


来源:https://stackoverflow.com/questions/4693968/is-there-an-existing-fileinputstream-delete-on-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!