|
@@ -2,13 +2,28 @@ package ieven.server.webapp.util;
|
|
|
|
|
|
import com.ibm.icu.text.CharsetDetector;
|
|
|
import com.ibm.icu.text.CharsetMatch;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
|
|
+import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class FileUtils {
|
|
|
+ private static final Logger logger = Logger.getLogger(FileUtils.class);
|
|
|
+
|
|
|
+ public FileUtils() {
|
|
|
+ }
|
|
|
|
|
|
public static ByteArrayOutputStream cloneInputStream(InputStream input) {
|
|
|
try {
|
|
@@ -29,7 +44,7 @@ public class FileUtils {
|
|
|
/**
|
|
|
* 读文件,根据文件名,返回文件内容字符串;
|
|
|
* 读文件之前会探测编码格式,按准确的编码格式进行读取;若编码格式探测失败,则默认按照"UTF-8"进行读取
|
|
|
- * */
|
|
|
+ */
|
|
|
public static String getFileCharsetByICU4J(FileInputStream inputStream) {
|
|
|
String encoding = "UTF-8";
|
|
|
try {
|
|
@@ -57,4 +72,263 @@ public class FileUtils {
|
|
|
}
|
|
|
return encoding;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * md5检验上传文件是否与历史文件重复
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param filePath
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<Boolean, String> distinctWithMd5(MultipartFile file, String filePath) {
|
|
|
+ boolean result = false;
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String md5 = new String();
|
|
|
+ try {
|
|
|
+ md5 = DigestUtils.md5Hex(file.getInputStream());
|
|
|
+
|
|
|
+ File uploadFilePath = new File(filePath);
|
|
|
+ File[] tempList = uploadFilePath.listFiles();
|
|
|
+ for (int i = 0; i < tempList.length; i++) {
|
|
|
+ if (tempList[i].isFile()) {
|
|
|
+ result = md5.equals(DigestUtils.md5Hex(new FileInputStream(tempList[i])));
|
|
|
+ if (result == true)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Map<Boolean, String> res = new HashMap<>();
|
|
|
+ res.put(result, fileName);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检验当前上传文件列表中的文件是否与历史冲突
|
|
|
+ *
|
|
|
+ * @param files
|
|
|
+ * @param fileUploadPath
|
|
|
+ * @return 将重复文件名返回,Boolean 标识是否含有重复文件
|
|
|
+ */
|
|
|
+ public static Map<Boolean, List<String>> filesRepetition(List<MultipartFile> files, String fileUploadPath) {
|
|
|
+ Map<Boolean, List<String>> result = new HashMap<>();
|
|
|
+ boolean repetitionExist = false;
|
|
|
+ List<String> repetitionFiles = new ArrayList<>();
|
|
|
+ files.forEach(x -> {
|
|
|
+ Map<Boolean, String> fileRepetition = distinctWithMd5(x, fileUploadPath);
|
|
|
+ if (fileRepetition.containsKey(true)) {
|
|
|
+ repetitionFiles.add(fileRepetition.get(true));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (!repetitionFiles.isEmpty()) {
|
|
|
+ repetitionExist = true;
|
|
|
+ }
|
|
|
+ result.put(repetitionExist, repetitionFiles);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将文件按照指定路径上传
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param savePath
|
|
|
+ */
|
|
|
+ public static boolean uploadFile(MultipartFile file, String savePath) {
|
|
|
+ //上传
|
|
|
+ File path = new File(savePath);
|
|
|
+ boolean status = true;
|
|
|
+ if (!path.getParentFile().exists()) {
|
|
|
+ try {
|
|
|
+ logger.info("父目录不存在,创建父目录: " + path.getParent());
|
|
|
+ if (path.getParentFile().mkdirs()) {
|
|
|
+ logger.info("上传文件 : " + savePath);
|
|
|
+ file.transferTo(new File(savePath));
|
|
|
+ } else {
|
|
|
+ logger.info("创建文件失败:" + path.getParent() + "无法创建!");
|
|
|
+ status = false;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ status = false;
|
|
|
+ }
|
|
|
+ return status;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ logger.info("上传文件 : " + savePath);
|
|
|
+ file.transferTo(new File(savePath));
|
|
|
+ return status;
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("文件上传错误", e);
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param fileName 要删除的文件名
|
|
|
+ * @return 删除成功返回true,否则返回false
|
|
|
+ */
|
|
|
+ public static boolean deleteFile(String fileName) {
|
|
|
+ File file = new File(fileName);
|
|
|
+ if (!file.exists()) {
|
|
|
+ logger.info("删除文件失败:" + fileName + "不存在!");
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ if (file.isFile()) {
|
|
|
+ return file.delete();
|
|
|
+ } else {
|
|
|
+ logger.info("删除文件失败:" + fileName + "不是文件!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读文件,根据文件名,返回文件内容字符串;
|
|
|
+ * 读文件之前会探测编码格式,按准确的编码格式进行读取;若编码格式探测失败,则默认按照"UTF-8"进行读取
|
|
|
+ */
|
|
|
+ public static String getFileCharsetByICU4J(File file) {
|
|
|
+ String encoding = null;
|
|
|
+ try {
|
|
|
+ Path path = Paths.get(file.getPath());
|
|
|
+ byte[] data = Files.readAllBytes(path);
|
|
|
+ BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
|
|
|
+ int p = (bin.read() << 8) + bin.read();
|
|
|
+ bin.close();
|
|
|
+ switch (p) {
|
|
|
+ case 0xefbb:
|
|
|
+ encoding = "UTF-8";
|
|
|
+ break;
|
|
|
+ case 0xfffe:
|
|
|
+ encoding = "Unicode";
|
|
|
+ break;
|
|
|
+ case 0xfeff:
|
|
|
+ encoding = "UTF-16BE";
|
|
|
+ break;
|
|
|
+ case 45801:
|
|
|
+ encoding = "GB18030";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ CharsetDetector detector = new CharsetDetector();
|
|
|
+ detector.setText(data);
|
|
|
+ CharsetMatch match = detector.detect();
|
|
|
+ if (match == null) {
|
|
|
+ encoding = "GBK";
|
|
|
+ } else {
|
|
|
+ encoding = match.getName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException var6) {
|
|
|
+ var6.printStackTrace();
|
|
|
+ encoding = "UTF-8";
|
|
|
+ }
|
|
|
+ return encoding;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void unPackRar(File rarFile, String destDir) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static void unPack7z(File file, String destDir) {
|
|
|
+ try {
|
|
|
+ SevenZFile sevenZFile = new SevenZFile(file);
|
|
|
+ SevenZArchiveEntry entry = sevenZFile.getNextEntry();
|
|
|
+ while (entry != null) {
|
|
|
+ File f = new File(destDir + File.separator + entry.getName());
|
|
|
+ if (entry.isDirectory()) {
|
|
|
+ if (!file.exists()) {
|
|
|
+ f.mkdirs();
|
|
|
+ }
|
|
|
+ entry = sevenZFile.getNextEntry();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!f.getParentFile().exists()) {
|
|
|
+ f.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream out = new FileOutputStream(file);
|
|
|
+ byte[] content = new byte[(int) entry.getSize()];
|
|
|
+ sevenZFile.read(content, 0, content.length);
|
|
|
+ out.write(content);
|
|
|
+ out.close();
|
|
|
+ entry = sevenZFile.getNextEntry();
|
|
|
+ }
|
|
|
+ sevenZFile.close();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static void search(File file, List<File> result) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File subFile : files) {
|
|
|
+ search(subFile, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String fileName = file.getName();
|
|
|
+ //System.out.println(fileName);
|
|
|
+ if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx") || fileName.endsWith(".csv")) {
|
|
|
+ result.add(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static List<MultipartFile> transferFileToMulti(List<File> files) {
|
|
|
+ List<MultipartFile> list = new ArrayList<>();
|
|
|
+ for (File file : files) {
|
|
|
+ list.add(getMultipartFile(file));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static MultipartFile getMultipartFile(File file) {
|
|
|
+ FileInputStream fileInputStream = null;
|
|
|
+ MultipartFile multipartFile = null;
|
|
|
+ try {
|
|
|
+ fileInputStream = new FileInputStream(file);
|
|
|
+ multipartFile = new MockMultipartFile(file.getName(), file.getName(),
|
|
|
+ ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return multipartFile;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void deleteAllFiles(File file) {
|
|
|
+ //判断文件不为null或文件目录存在
|
|
|
+ if (file == null || !file.exists()) {
|
|
|
+ logger.info("文件删除失败,请检查文件路径是否正确");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //取得这个目录下的所有子文件对象
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ //遍历该目录下的文件对象
|
|
|
+ for (File f : files) {
|
|
|
+ //打印文件名
|
|
|
+ String name = file.getName();
|
|
|
+ System.out.println(name);
|
|
|
+ //判断子目录是否存在子目录,如果是文件则删除
|
|
|
+ if (f.isDirectory()) {
|
|
|
+ deleteAllFiles(f);
|
|
|
+ } else {
|
|
|
+ f.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除空文件夹 for循环已经把上一层节点的目录清空。
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
}
|