Java 在tomcat上保存临时文件的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23775620/
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
Best practice to save temp files on tomcat?
提问by Steve Waters
I need to demo a xml based data interchange system. It is demoed offline on a trusted computer at school. The application will get database later but just for this presentation I only need to show the layout, opening and saving of xml-files and how the table gets data from the xml.
我需要演示一个基于 xml 的数据交换系统。它在学校受信任的计算机上离线演示。该应用程序稍后将获取数据库,但仅在本演示文稿中,我只需要显示 xml 文件的布局、打开和保存以及表如何从 xml 获取数据。
So, what would be the best location to let the web app create the xml files temporarily so I can showcase this app? I'm using Eclipse and Tomcat.
那么,让 Web 应用程序临时创建 xml 文件以便我可以展示此应用程序的最佳位置是什么?我正在使用 Eclipse 和 Tomcat。
As mentioned, security is not an issue at all, since this version is NOT going to be online. Also, it's ok if the files get erased each time the application is run.
如前所述,安全性根本不是问题,因为此版本不会在线。此外,如果每次运行应用程序时文件都被删除,那也没关系。
They need to exist only for the duration of the presentation where the application is run once. So, I'm clueless what would be the best location and how to get the path to such location not depending on the computer used.
它们只需要在应用程序运行一次的演示期间存在。所以,我不知道什么是最好的位置,以及如何在不依赖于所使用的计算机的情况下获得通往该位置的路径。
Thank you.
谢谢你。
采纳答案by Omoro
Use the property java.io.tmpdir
to get the tomcat
temp folder and save your files there. That will be a good place to temporarily keep them in. So you can define it as follows:
使用该属性java.io.tmpdir
获取tomcat
临时文件夹并将文件保存在那里。那将是暂时保留它们的好地方。因此您可以将其定义如下:
public static final String TMP_DIR = System.getProperty("java.io.tmpdir")
回答by Cédric Couralet
Either use the property java.io.tmpdir
as indicated or use the servlet context attribute javax.servlet.context.tempdir
defined in the specification. For tomcat that attribute can be changed by the workDir attribute on the context.
使用指定的属性java.io.tmpdir
或使用javax.servlet.context.tempdir
规范中定义的 servlet 上下文属性。对于 tomcat,该属性可以通过上下文中的 workDir 属性更改。
You can use that attribute by a call to servletContext.getAttribute("javax.servlet.context.tempdir")
您可以通过调用来使用该属性 servletContext.getAttribute("javax.servlet.context.tempdir")
回答by rohit
You can use this method to get temp
file location according to your Operating System
您可以使用此方法temp
根据您的操作系统获取文件位置
public static String getSystemFileLocation()
{
File tmpFile = null;
String fileLocation="/tmp/";
if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") == 0) {
tmpFile=new File("c:\temp");
if(tmpFile.exists()) {
fileLocation="c:\temp\";
}
else {
tmpFile.mkdir();
fileLocation="c:\temp\";
}
}
else if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
fileLocation="/tmp/";
}
return fileLocation;
}
回答by oak
Old question, but if there is for creating temp file why not use File.createTempFile
function?
老问题,但如果有用于创建临时文件为什么不使用 File.createTempFile
函数?
Example:
例子:
File temp = File.createTempFile("real",".howto");
temp.deleteOnExit();