|
|
|
@ -9,24 +9,50 @@ import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
import org.apache.poi.util.DefaultTempFileCreationStrategy; |
|
|
|
|
import org.apache.poi.util.TempFile; |
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.exception.ExcelAnalysisException; |
|
|
|
|
import com.alibaba.excel.exception.ExcelGenerateException; |
|
|
|
|
import com.alibaba.excel.exception.ExcelCommonException; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* @author jipengfei |
|
|
|
|
*/ |
|
|
|
|
public class FileUtils { |
|
|
|
|
public static final String POI_FILES = "poifiles"; |
|
|
|
|
public static final String EX_CACHE = "excache"; |
|
|
|
|
/** |
|
|
|
|
* If a server has multiple projects in use at the same time, a directory with the same name will be created under |
|
|
|
|
* the temporary directory, but each project is run by a different user, so there is a permission problem, so each |
|
|
|
|
* project creates a unique UUID as a separate Temporary Files. |
|
|
|
|
*/ |
|
|
|
|
private static String tempFilePrefix = |
|
|
|
|
System.getProperty(TempFile.JAVA_IO_TMPDIR) + File.separator + UUID.randomUUID().toString() + File.separator; |
|
|
|
|
/** |
|
|
|
|
* Used to store poi temporary files. |
|
|
|
|
*/ |
|
|
|
|
private static String poiFilesPath = tempFilePrefix + POI_FILES + File.separator; |
|
|
|
|
/** |
|
|
|
|
* Used to store easy excel temporary files. |
|
|
|
|
*/ |
|
|
|
|
private static String cachePath = tempFilePrefix + EX_CACHE + File.separator; |
|
|
|
|
|
|
|
|
|
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; |
|
|
|
|
|
|
|
|
|
private static final String POIFILES = "poifiles"; |
|
|
|
|
|
|
|
|
|
private static final String CACHE = "excache"; |
|
|
|
|
private static final int WRITE_BUFF_SIZE = 8192; |
|
|
|
|
|
|
|
|
|
private FileUtils() {} |
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
// Create a temporary directory in advance
|
|
|
|
|
File tempFile = new File(tempFilePrefix); |
|
|
|
|
createDirectory(tempFile); |
|
|
|
|
tempFile.deleteOnExit(); |
|
|
|
|
// Initialize the cache directory
|
|
|
|
|
File cacheFile = new File(cachePath); |
|
|
|
|
createDirectory(cacheFile); |
|
|
|
|
cacheFile.deleteOnExit(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Reads the contents of a file into a byte array. * The file is always closed. |
|
|
|
|
* |
|
|
|
@ -106,19 +132,31 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
*/ |
|
|
|
|
public static void createPoiFilesDirectory() { |
|
|
|
|
createTmpDirectory(POIFILES); |
|
|
|
|
File poiFilesPathFile = new File(poiFilesPath); |
|
|
|
|
createDirectory(poiFilesPathFile); |
|
|
|
|
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(poiFilesPathFile)); |
|
|
|
|
poiFilesPathFile.deleteOnExit(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static File createCacheTmpFile() { |
|
|
|
|
File directory = createTmpDirectory(CACHE); |
|
|
|
|
File cache = new File(directory.getPath(), UUID.randomUUID().toString()); |
|
|
|
|
if (!cache.mkdir()) { |
|
|
|
|
throw new ExcelGenerateException("Can not create temp file!"); |
|
|
|
|
return createDirectory(new File(cachePath + UUID.randomUUID().toString())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static File createTmpFile(String fileName) { |
|
|
|
|
File directory = createDirectory(new File(tempFilePrefix)); |
|
|
|
|
return new File(directory, fileName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* @param directory |
|
|
|
|
*/ |
|
|
|
|
private static File createDirectory(File directory) { |
|
|
|
|
if (!directory.exists() && !directory.mkdirs()) { |
|
|
|
|
throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath()); |
|
|
|
|
} |
|
|
|
|
return cache; |
|
|
|
|
return directory; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -144,35 +182,27 @@ public class FileUtils {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static File createTmpDirectory(String path) { |
|
|
|
|
String tmpDir = System.getProperty(JAVA_IO_TMPDIR); |
|
|
|
|
if (tmpDir == null) { |
|
|
|
|
throw new RuntimeException( |
|
|
|
|
"Systems temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!"); |
|
|
|
|
} |
|
|
|
|
File directory = new File(tmpDir, path); |
|
|
|
|
if (!directory.exists()) { |
|
|
|
|
syncCreatePoiFilesDirectory(directory); |
|
|
|
|
} |
|
|
|
|
return directory; |
|
|
|
|
public static String getTempFilePrefix() { |
|
|
|
|
return tempFilePrefix; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static File createTmpFile(String fileName) { |
|
|
|
|
String tmpDir = System.getProperty(JAVA_IO_TMPDIR); |
|
|
|
|
if (tmpDir == null) { |
|
|
|
|
throw new RuntimeException( |
|
|
|
|
"Systems temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!"); |
|
|
|
|
} |
|
|
|
|
return new File(tmpDir, fileName); |
|
|
|
|
public static void setTempFilePrefix(String tempFilePrefix) { |
|
|
|
|
FileUtils.tempFilePrefix = tempFilePrefix; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* @param directory |
|
|
|
|
*/ |
|
|
|
|
private static synchronized void syncCreatePoiFilesDirectory(File directory) { |
|
|
|
|
if (!directory.exists()) { |
|
|
|
|
directory.mkdirs(); |
|
|
|
|
} |
|
|
|
|
public static String getPoiFilesPath() { |
|
|
|
|
return poiFilesPath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void setPoiFilesPath(String poiFilesPath) { |
|
|
|
|
FileUtils.poiFilesPath = poiFilesPath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getCachePath() { |
|
|
|
|
return cachePath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void setCachePath(String cachePath) { |
|
|
|
|
FileUtils.cachePath = cachePath; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|