Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving files to temp folder

Tags:

java

Working on a Java(Spring) based application. I have written a method that saves all the data in a table to a corresponding xls sheet. Right now, I am hard coding the directory location and saving the generated xls there. Is there a way to make it store to the temp location of the user's system. Assuming different file system would have different temp locations.

like image 252
Forkmohit Avatar asked Jul 01 '15 07:07

Forkmohit


2 Answers

you can use System.getProperty("java.io.tmpdir") to get the temporary directory of the environment that is running the application (if you run the application under tomcat you get the tomcat temp folder, if you run the program under the command line you get the folder specified by o.s. environment variable).

If your application is a web one running inside a web container you can append the user name or user id to the file name to distinguish file for different users.

like image 100
Giovanni Avatar answered Sep 20 '22 12:09

Giovanni


Different properties are defined in System.getProperty(x) as defined here http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties%28%29

"java.io.tmpdir"    Default temp file path user.home
"user.home"         User's home directory
"user.dir"          User's current working directory
"user.name"         User account name
like image 37
Danielson Avatar answered Sep 22 '22 12:09

Danielson