简体   繁体   中英

Saving files to temp folder

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.

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 os 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM