Search code examples
javaservletstemporary-files

How do you clean up a temp file that you created in a servlet and returned to the user?


I have a servlet that does the following:

  1. User calls the servlet
  2. Servlet code calls a web service to download a PDF file from an Adobe Live Cycle server.
  3. Saves that PDF to a temp file.
  4. Calls ps2pdf14 to convert that PDF to an ancient PDF 1.4 version (which creates a second temporary file)
  5. Returns the contents of file2 as the response

What is the best way to cleanup these files?

Batch process?

Any ideas?


Solution

  • Few options-

    1. Delete the files as soon as you send the response
    2. Run a background (housekeeping) job (some daemon thread) that would periodically delete such temp files
    3. Do not write anything to the disk, perform the conversion operation in the memory and return the modified contents to the user directly (not sure if this is feasible)