forked from fanruan/easyexcel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.2 KiB
116 lines
3.2 KiB
7 years ago
|
package com.alibaba.excel.util;
|
||
|
|
||
|
import java.io.File;
|
||
6 years ago
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.OutputStream;
|
||
6 years ago
|
import java.util.UUID;
|
||
7 years ago
|
|
||
6 years ago
|
import com.alibaba.excel.exception.ExcelAnalysisException;
|
||
|
|
||
7 years ago
|
/**
|
||
|
*
|
||
|
* @author jipengfei
|
||
|
*/
|
||
6 years ago
|
public class FileUtils {
|
||
7 years ago
|
|
||
|
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
|
||
|
|
||
|
private static final String POIFILES = "poifiles";
|
||
|
|
||
6 years ago
|
private static final String CACHE = "excache";
|
||
|
|
||
6 years ago
|
/**
|
||
|
* Write inputStream to file
|
||
|
*
|
||
|
* @param file
|
||
|
* @param inputStream
|
||
|
*/
|
||
|
public static void writeToFile(File file, InputStream inputStream) {
|
||
|
OutputStream outputStream = null;
|
||
|
try {
|
||
|
outputStream = new FileOutputStream(file);
|
||
|
int bytesRead;
|
||
|
byte[] buffer = new byte[8192];
|
||
|
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
|
||
|
outputStream.write(buffer, 0, bytesRead);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
throw new ExcelAnalysisException("Can not create temporary file!", e);
|
||
|
} finally {
|
||
|
if (outputStream != null) {
|
||
|
try {
|
||
|
outputStream.close();
|
||
|
} catch (IOException e) {
|
||
|
throw new ExcelAnalysisException("Can not close 'outputStream'!", e);
|
||
|
}
|
||
|
}
|
||
|
if (inputStream != null) {
|
||
|
try {
|
||
|
inputStream.close();
|
||
|
} catch (IOException e) {
|
||
|
throw new ExcelAnalysisException("Can not close 'inputStream'", e);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
*/
|
||
6 years ago
|
public static void createPoiFilesDirectory() {
|
||
6 years ago
|
createTmpDirectory(POIFILES);
|
||
|
}
|
||
|
|
||
|
public static File createCacheTmpFile() {
|
||
|
File directory = createTmpDirectory(CACHE);
|
||
|
return new File(directory.getPath(), UUID.randomUUID().toString());
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
/**
|
||
|
* delete file
|
||
|
*
|
||
|
* @param file
|
||
|
*/
|
||
|
public static void delete(File file) {
|
||
|
if (file.isFile()) {
|
||
|
file.delete();
|
||
|
return;
|
||
|
}
|
||
|
if (file.isDirectory()) {
|
||
|
File[] childFiles = file.listFiles();
|
||
|
if (childFiles == null || childFiles.length == 0) {
|
||
|
file.delete();
|
||
|
return;
|
||
|
}
|
||
|
for (int i = 0; i < childFiles.length; i++) {
|
||
|
delete(childFiles[i]);
|
||
|
}
|
||
|
file.delete();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static File createTmpDirectory(String path) {
|
||
7 years ago
|
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!");
|
||
|
}
|
||
6 years ago
|
File directory = new File(tmpDir, path);
|
||
7 years ago
|
if (!directory.exists()) {
|
||
|
syncCreatePOIFilesDirectory(directory);
|
||
|
}
|
||
6 years ago
|
return directory;
|
||
7 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param directory
|
||
|
*/
|
||
|
private static synchronized void syncCreatePOIFilesDirectory(File directory) {
|
||
|
if (!directory.exists()) {
|
||
|
directory.mkdirs();
|
||
|
}
|
||
|
}
|
||
|
}
|