java - Deleting temporary directory -


i found code on here creating temporary directories in java.

public static file createtempdirectory() throws ioexception {     final file temp;     temp = file.createtempfile("temp", long.tostring(system.nanotime()));      if(!(temp.delete()))     {         throw new ioexception("could not delete temp file: " + temp.getabsolutepath());     }     if(!(temp.mkdir()))     {         throw new ioexception("could not create temp directory: " + temp.getabsolutepath());     }     return temp; } 

how can @ end of servlet's life handle on temporary directory , delete it?

first:

don't use method of creating temporary directory! it unsafe! use guava method files.createtempdir() instead (or re-implement manually, if don't want use guava). reason described in javadoc:

a common pitfall call createtempfile, delete file , create directory in place, leads race condition can exploited create security vulnerabilities, when executable files written directory.

regarding real question:

you need delete directory manually, means need keep track of directories create (for example in collection<file>) , delete them when know sure not longer needed.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -