java 将文件保存到临时文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31154727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:11:43  来源:igfitidea点击:

Saving files to temp folder

java

提问by Forkmohit

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.

在基于 Java(Spring) 的应用程序上工作。我编写了一个方法,将表中的所有数据保存到相应的 xls 表中。现在,我正在对目录位置进行硬编码并将生成的 xls 保存在那里。有没有办法让它存储到用户系统的临时位置。假设不同的文件系统会有不同的临时位置。

回答by Giovanni

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

您可以使用System.getProperty("java.io.tmpdir")获取运行应用程序的环境的临时目录(如果您在tomcat下运行应用程序,您将获得tomcat temp文件夹,如果您在命令行下运行程序,您将获得os环境变量指定的文件夹) .

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.

如果您的应用程序是在 Web 容器内运行的 Web 应用程序,您可以将用户名或用户 ID 附加到文件名以区分不同用户的文件。

回答by Danielson

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

System.getProperty(x) 中定义了不同的属性,如此处定义的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