JKowal34
JKowal34

Reputation: 25

How to create temporary file in and delete after exit from VM

I have spring mvc application and I need to return a preparated file to user as response on his request. The flow:

  1. user make request
  2. Base on request's data (json etc) I need to take a zip file, open there txt file inside zip change something in the file.
  3. Return modified zip.

My solution: I have base zip file on my server, when user make a request, I'm copy base file to tmp file -> I unzip tmp file -> make modification -> zip it again -> returning to the user.

Problem: How to sure that tmp file will be deleted after I process the request (even if error happen in VM)?

There are many users at the same time.

The size of zip is more than 100M.

Upvotes: 0

Views: 283

Answers (1)

O. Schnieders
O. Schnieders

Reputation: 681

https://howtodoinjava.com/java/io/how-to-delete-temporary-file-in-java/

here you have a good tutorial. Make sure that you call

File.deleteOnExit();

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates

See https://docs.oracle.com/javase/8/docs/api/java/io/File.html#deleteOnExit--

Upvotes: 1

Related Questions