java 更改使用 Apache POI 创建的临时文件的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5297911/
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
changing location of temp files created using Apache POI
提问by ronak.patel
I am stuck with an issue with reading .xlsx file. Some temporary files with random name are created under /tmp/poifiles directory whenever I use WorkbookFactory.create(inputStream);. This directory is created with RW-R-R- permission for the first user. So another user on the same machine when tries to access these files, he CANNOT.
我在阅读 .xlsx 文件时遇到了问题。每当我使用 WorkbookFactory.create(inputStream); 时,都会在 /tmp/poifiles 目录下创建一些具有随机名称的临时文件。该目录是使用第一个用户的 RW-RR- 权限创建的。因此,当同一台机器上的另一个用户尝试访问这些文件时,他不能。
Please suggest me any way
请以任何方式建议我
1) How can I create these temp files under /tmp directory and not always in /tmp/poifiles (I am using RHEL V5.0)
1) 如何在 /tmp 目录下而不总是在 /tmp/poifiles 中创建这些临时文件(我使用的是 RHEL V5.0)
2) and how can I configure POI such as to change the location from where it reads the temporary files??
2) 以及如何配置 POI,例如更改读取临时文件的位置?
Anymore help to solve my problem of different users accessing same .xlsx files through POI is badly needed.
非常需要帮助解决不同用户通过 POI 访问相同 .xlsx 文件的问题。
回答by ronak.patel
Yuppie...I got the solution....
雅皮士...我得到了解决方案....
POI uses the following method to create temp files.
POI 使用以下方法创建临时文件。
public static File createTempFile(String prefix, String suffix)
{
if (dir == null) {
dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
dir.mkdir();
if (System.getProperty("poi.keep.tmp.files") == null) {
dir.deleteOnExit();
}
}
File newFile = new File(dir, prefix + rnd.nextInt() + suffix);
if (System.getProperty("poi.keep.tmp.files") == null) {
newFile.deleteOnExit();
}
return newFile;
}
Now here as we can see it gets the location from property "java.io.tmpdir" and creates poifiles directory inside that...
现在在这里我们可以看到它从属性“java.io.tmpdir”获取位置并在其中创建 poifiles 目录......
I changed the location of java.io.tmpdir by setting this property (using System.setProperty("java.io.tmpdir", "somepath"))to user specific location..and Voila....Every user now can create temp files at location always accessible to them and not only the first user gets the privilege to create directory accessible only to him ...!!!
我通过将此属性(使用 System.setProperty("java.io.tmpdir", "somepath"))设置为用户特定位置来更改 java.io.tmpdir 的位置..和瞧..每个用户现在都可以创建临时文件在他们始终可以访问的位置,不仅第一个用户有权创建只有他才能访问的目录......!!!
回答by D.Shev
Here is how you can change the location from where POI reads the temporary files programmatically if you are not able to change system property "java.io.tmpdir"
如果您无法更改系统属性“java.io.tmpdir”,您可以通过以下方式更改 POI 以编程方式读取临时文件的位置
File dir = new File("somepath");
dir.mkdir();
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(dir));
This is driven by the Apache POI TempFileand DefaultTempFileCreationStrategyhelper classes.
这是由 Apache POI TempFile和DefaultTempFileCreationStrategy辅助类驱动的。