How to delete PrimeFaces UploadedFile temp file after processing?

java, jsf, primefaces

Solution

PrimeFaces uses Apache Commons FileUpload under the covers for this. It will create the file as a temporary file and hence the file will already be automatically deleted if there are no open `File` nor `InputStream` references to it when the Java Garbage Collector runs.

So if you can make absolutely sure that you close the `InputStream` after processing (in the `finally` block!), then you don't need to worry about cleanup.

Problem

PrimeFaces UploadedFile only exposes the InputStream, not the File itself. How can I delete it after processing the stream?

Original source