许鹏程
2025-02-10 ef26bf4fde77ba361bcd083a769c3a1b8a229738
src/main/java/com/product/file/util/FileUtil.java
@@ -3,8 +3,6 @@
import cn.hutool.core.lang.UUID;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.google.common.collect.Maps;
import com.product.common.lang.StringUtils;
import com.product.core.config.Global;
@@ -16,7 +14,6 @@
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -33,6 +30,47 @@
   private FileUtil() {
   }
   public static void main(String[] args) {
      System.out.println(FileUtil.checkDocType(new File("D:\\downloads\\关于协调解决西宁万达地产置业有限公司商品房预售资金监管账户冻结相关问题 (1).doc")));
   }
   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文件的头部也是D0CF11E0,所以需要判断文件后缀
            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文件的头部也是D0CF11E0,所以需要判断文件后缀
            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();
   }
   /**
    * 合并word文档
    *
@@ -41,38 +79,7 @@
    * @return
    */
   public static File mergeFile(File source, File target) throws Exception {
      //判断文件是否为 docx或doc格式
      if (!source.getName().endsWith(".docx") && !source.getName().endsWith(".doc")) {
         throw new BaseException(FileCode.ADD_FILE_NOT_ALLOWED);
      }
      //如果是doc格式则转换为docx格式
      if (source.getName().endsWith(".doc")) {
         source = toDocx(source);
      }
      if (target.getName().endsWith(".doc")) {
         target = toDocx(target);
      }
      XWPFTemplate template = XWPFTemplate.compile(source);
      NiceXWPFDocument xwpfDocument = template.getXWPFDocument();
      try {
         NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream((target)));
         NiceXWPFDocument newDoc = xwpfDocument.merge(sub);
         String tempFile = Global.getSystemConfig("temp.dir", "./attachment/temp") + File.separator + UUID.randomUUID() + ".docx";
         try (FileOutputStream out = new FileOutputStream(tempFile)) {
            newDoc.write(out);
            newDoc.close();
            return new File(tempFile);
         } catch (IOException e) {
            throw e;
         } finally {
            newDoc.close();
            sub.close();
         }
      } catch (IOException e) {
         throw e;
      } catch (Exception e) {
         throw e;
      }
      return MergeWordDocuments.mergeFile(source, target);
   }
   public static File toDocx(File file) {
@@ -83,8 +90,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) {