make the default temporary directory contain the username to avoid permission issues when deploying applications on the same server with multiple users

This commit is contained in:
zhaohc 2024-09-27 20:40:28 +08:00
parent 8235db8758
commit ea0020dbda
1 changed files with 11 additions and 1 deletions

View File

@ -47,6 +47,9 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
/** To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property */ /** To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property */
public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit"; public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit";
/** JVM property for the current username. */
private static final String JAVA_PROP_USER_NAME = "user.name";
/** The directory where the temporary files will be created (<code>null</code> to use the default directory). */ /** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
private volatile File dir; private volatile File dir;
@ -85,10 +88,17 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
if (tmpDir == null) { if (tmpDir == null) {
throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!"); throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!");
} }
String poifilesDir = POIFILES;
// Make the default temporary directory contain the username to avoid permission issues
// when deploying applications on the same server with multiple users
String username = System.getProperty(JAVA_PROP_USER_NAME);
if (null != username && !username.isEmpty()) {
poifilesDir += "_" + username;
}
dirLock.lock(); dirLock.lock();
try { try {
if (dir == null) { if (dir == null) {
Path dirPath = Paths.get(tmpDir, POIFILES); Path dirPath = Paths.get(tmpDir, poifilesDir);
File fileDir = dirPath.toFile(); File fileDir = dirPath.toFile();
if (fileDir.exists()) { if (fileDir.exists()) {
if (!fileDir.isDirectory()) { if (!fileDir.isDirectory()) {