• 0

[JAVA] Delete temporary files when...


Question

I am working on a page for my employer and I need a little help if you don't mind :D

Right now there is an application running that sends a bunch of charcters to a JSP page when a button is clicked (that is the best description of that). What I do is grab those characters and create a new temporary file so the person who clicked the button can download it.

Here is what I do:

...
File dir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir");
File temp = File.createTempFile(item.getName(), "", dir);
temp.deleteOnExit();
...
while((line = in.readLine()) != null)
	fout.write(line + "\r\n");
fout.close();
...

Everything works GREAT. Yippy, I am happy...

BUT this is my issue. The file is no longer temporary. The file will stick around until the JVM is killed. We don't restart our server that often, so the files just build up and stick around for a while.

I know I can create (or find) an application that watches the directory on the server and deletes file that are more than....say....20 minutes old, but I would like to know if anyone knows how to delete the files sooner.

Like, is there a way to delete the files when garbage collection runs or when the JSP session is closed?

Thanks Neowinians, you all rock! :)

Link to comment
https://www.neowin.net/forum/topic/474685-java-delete-temporary-files-when/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Never mind, I'm a moron....

javax.servlet.http.HttpSessionListener

Using the sessionCreated method I will create folder in the temp directory with the sessions ID name.

Using the sessionDestroyed method I will delete the folder and it's contents.

DUH!

Thanks guys :woot:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.