From a7e5e29bd9771b9cc65cfe09d213c470bbe4166b Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期三, 17 七月 2024 18:28:11 +0800 Subject: [PATCH] 添加pom版本号 --- src/main/java/com/product/file/util/FileUtil.java | 68 +++++++++++++++++++++++---------- 1 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/product/file/util/FileUtil.java b/src/main/java/com/product/file/util/FileUtil.java index 6275477..1b1e553 100644 --- a/src/main/java/com/product/file/util/FileUtil.java +++ b/src/main/java/com/product/file/util/FileUtil.java @@ -1,8 +1,6 @@ package com.product.file.util; import cn.hutool.core.lang.UUID; -import cn.hutool.core.util.IdUtil; -import cn.hutool.poi.word.WordUtil; import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.deepoove.poi.XWPFTemplate; @@ -14,10 +12,6 @@ import com.product.file.config.CmnConst; import com.product.file.config.FileCode; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.text.WordUtils; -import org.apache.log4j.Logger; -import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.*; import java.util.List; @@ -35,10 +29,45 @@ */ @Slf4j public class FileUtil { - private static Properties properties; - private FileUtil() { + } + + public static String checkDocType(File file) { + try (FileInputStream fis = new FileInputStream(file)) { + byte[] bytes = new byte[8]; + fis.read(bytes, 0, 8); + + String hex = bytesToHex(bytes); + + if (hex.contains("504B0304") && (file.getName().endsWith(".doc") || file.getName().endsWith(".docx"))) { + return "docx"; + } else if (hex.contains("D0CF11E0") && (file.getName().endsWith(".doc") || file.getName().endsWith(".docx"))) { + //鍥犱负doc鏂囦欢鐨勫ご閮ㄤ篃鏄疍0CF11E0锛屾墍浠ラ渶瑕佸垽鏂枃浠跺悗缂� + return "doc"; + } + //澧炲姞xls 鍜� xlsx鐨勫垽鏂� + else if (hex.contains("504B0304") && (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx"))) { + return "xlsx"; + } else if (hex.contains("D0CF11E0") && (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx"))) { + //鍥犱负xls鏂囦欢鐨勫ご閮ㄤ篃鏄疍0CF11E0锛屾墍浠ラ渶瑕佸垽鏂枃浠跺悗缂� + return "xls"; + } else { + return "unknown"; + } + } catch ( + IOException e) { + e.printStackTrace(); + return "unknown"; + } + } + + private static String bytesToHex(byte[] bytes) { + StringBuilder hex = new StringBuilder(); + for (byte b : bytes) { + hex.append(String.format("%02X", b)); + } + return hex.toString(); } /** @@ -49,16 +78,13 @@ * @return */ public static File mergeFile(File source, File target) throws Exception { - //鍒ゆ柇鏂囦欢鏄惁涓� docx鎴杁oc鏍煎紡 - if (!source.getName().endsWith(".docx") && !source.getName().endsWith(".doc")) { - throw new BaseException(FileCode.ADD_FILE_NOT_ALLOWED); - } - //濡傛灉鏄痙oc鏍煎紡鍒欒浆鎹负docx鏍煎紡 - if (source.getName().endsWith(".doc")) { + if ("doc".equals(FileUtil.checkDocType(source))) { source = toDocx(source); } - if (target.getName().endsWith(".doc")) { + boolean delTarget = false; + if ("doc".equals(FileUtil.checkDocType(target))) { target = toDocx(target); + delTarget = true; } XWPFTemplate template = XWPFTemplate.compile(source); NiceXWPFDocument xwpfDocument = template.getXWPFDocument(); @@ -73,6 +99,9 @@ } catch (IOException e) { throw e; } finally { + if (delTarget) { + target.delete(); + } newDoc.close(); sub.close(); } @@ -91,8 +120,9 @@ File docxFile = new File(file.getParent() + File.separator + docxName); String tempDir = Global.getSystemConfig("temp.dir", "./attachment/temp"); File tempFile = new File(tempDir + File.separator + docxName); - try { - Document document = new Document(new FileInputStream(file)); + + try (FileInputStream is = new FileInputStream(file);) { + Document document = new Document(is); document.save(tempFile.getAbsolutePath(), com.aspose.words.SaveFormat.DOCX); return tempFile; } catch (Exception e) { @@ -127,7 +157,6 @@ * @return zip鏂囦欢淇濆瓨缁濆璺緞 */ public static long createZip(List<Map<String, String>> pathList, OutputStream os) { - log.info("姝e湪鎵撳寘鏂囦欢..."); try { long size = 0; ZipOutputStream out = new ZipOutputStream(os); @@ -152,7 +181,6 @@ fileName = head + "(" + showCount + ")." + tail; } fileNameCountMap.put(fileName, ++showCount); - log.info(String.format("姝e湪鎵撳寘鏂囦欢 %s", fileName)); //浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕 FileInputStream fis = new FileInputStream(downloadPath); @@ -164,11 +192,9 @@ while ((len = fis.read(buffer)) > 0) { if (symbol == 1) { // 鍔犲瘑 - log.info("姝e湪鍔犲瘑..."); out.write(encryption(buffer), 0, len); } else if (symbol == -1) { // 瑙e瘑 - log.info("姝e湪瑙e瘑..."); out.write(decryption(buffer), 0, len); } else { // 鍗曠函鐨勫鍒� -- Gitblit v1.9.2