From cb0fa7a50d7d4c17b086c29789ab9fad67190684 Mon Sep 17 00:00:00 2001
From: 许鹏程 <1821349743@qq.com>
Date: 星期五, 28 七月 2023 11:23:36 +0800
Subject: [PATCH] 套红

---
 src/main/java/com/product/file/util/FileUtil.java |  362 ++++++++++++++++++++++++++++++---------------------
 pom.xml                                           |    5 
 2 files changed, 219 insertions(+), 148 deletions(-)

diff --git a/pom.xml b/pom.xml
index fadae88..ebf6192 100644
--- a/pom.xml
+++ b/pom.xml
@@ -29,6 +29,11 @@
             <groupId>com.lx</groupId>
             <artifactId>product-server-core</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>com.deepoove</groupId>
+            <artifactId>poi-tl</artifactId>
+        </dependency>
         <!--浣跨敤open office-->
         <dependency>
             <groupId>com.artofsolving</groupId>
diff --git a/src/main/java/com/product/file/util/FileUtil.java b/src/main/java/com/product/file/util/FileUtil.java
index d6f9688..33a326b 100644
--- a/src/main/java/com/product/file/util/FileUtil.java
+++ b/src/main/java/com/product/file/util/FileUtil.java
@@ -1,11 +1,22 @@
 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.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;
 import com.product.core.exception.BaseException;
 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;
@@ -21,167 +32,222 @@
  * @Author: 6c
  * @Description:
  */
+@Slf4j
 public class FileUtil {
-    private static Logger logger = Logger.getLogger(FileUtil.class);
-    private static Properties properties;
+	private static Properties properties;
 
 
+	private FileUtil() {
+	}
 
-    private FileUtil() {
-    }
+	/**
+	 * 鍚堝苟word鏂囨。
+	 *
+	 * @param source 婧愭枃浠�
+	 * @param target 鐩爣鏂囦欢
+	 * @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")) {
+			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;
+		}
+	}
 
+	public static File toDocx(File file) {
+		String docxName = UUID.randomUUID().toString() + ".docx";
+		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));
+			document.save(tempFile.getAbsolutePath(), com.aspose.words.SaveFormat.DOCX);
+			return tempFile;
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return docxFile;
+	}
 
-    /**
-     * 鎵归噺鎵撳寘
-     *
-     * @param pathList 鏂囦欢璺緞闆嗗悎
-     * @param os       杈撳嚭娴�
-     * @return zip鏂囦欢淇濆瓨缁濆璺緞
-     */
-    public static long createZip(List<Map<String, String>> pathList, OutputStream os) {
-        logger.info("姝e湪鎵撳寘鏂囦欢...");
-        try {
-            long size = 0;
-            ZipOutputStream out = new ZipOutputStream(os);
+	/**
+	 * 鎵归噺鎵撳寘
+	 *
+	 * @param pathList 鏂囦欢璺緞闆嗗悎
+	 * @param os       杈撳嚭娴�
+	 * @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);
 
-            String downloadPath;
-            int symbol;
-            Map<String, Integer> fileNameCountMap = Maps.newHashMap();
-            int showCount = 0;
-            String fileName;
-            String head;
-            String tail;
-            for (Map<String, String> pathMap : pathList) {
-                downloadPath = pathMap.get(CmnConst.SOURCE_PATH);
-                symbol = "1".equals(pathMap.get(CmnConst.ENCRPT_SIGN)) ? -1 : 0;
+			String downloadPath;
+			int symbol;
+			Map<String, Integer> fileNameCountMap = Maps.newHashMap();
+			int showCount = 0;
+			String fileName;
+			String head;
+			String tail;
+			for (Map<String, String> pathMap : pathList) {
+				downloadPath = pathMap.get(CmnConst.SOURCE_PATH);
+				symbol = "1".equals(pathMap.get(CmnConst.ENCRPT_SIGN)) ? -1 : 0;
 
-                //鑾峰緱鏂囦欢鍚�
-                fileName = pathMap.get(CmnConst.REAL_FILE_NAME);
-                head = fileName.substring(0, fileName.lastIndexOf("."));
-                tail = fileName.substring(fileName.lastIndexOf(".") + 1);
-                showCount = fileNameCountMap.get(fileName) == null ? 0 : fileNameCountMap.get(fileName);
-                if (showCount > 0) {
-                    fileName = head + "(" + showCount + ")." + tail;
-                }
-                fileNameCountMap.put(fileName, ++showCount);
-                logger.info(String.format("姝e湪鎵撳寘鏂囦欢 %s", fileName));
+				//鑾峰緱鏂囦欢鍚�
+				fileName = pathMap.get(CmnConst.REAL_FILE_NAME);
+				head = fileName.substring(0, fileName.lastIndexOf("."));
+				tail = fileName.substring(fileName.lastIndexOf(".") + 1);
+				showCount = fileNameCountMap.get(fileName) == null ? 0 : fileNameCountMap.get(fileName);
+				if (showCount > 0) {
+					fileName = head + "(" + showCount + ")." + tail;
+				}
+				fileNameCountMap.put(fileName, ++showCount);
+				log.info(String.format("姝e湪鎵撳寘鏂囦欢 %s", fileName));
 
-                //浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕
-                FileInputStream fis = new FileInputStream(downloadPath);
-                out.putNextEntry(new ZipEntry(fileName));
+				//浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕
+				FileInputStream fis = new FileInputStream(downloadPath);
+				out.putNextEntry(new ZipEntry(fileName));
 
-                //鍐欏叆鍘嬬缉鍖�
-                int len;
-                byte[] buffer = new byte[1024];
-                while ((len = fis.read(buffer)) > 0) {
-                    if (symbol == 1) {
-                        // 鍔犲瘑
-                        logger.info("姝e湪鍔犲瘑...");
-                        out.write(encryption(buffer), 0, len);
-                    } else if (symbol == -1) {
-                        // 瑙e瘑
-                        logger.info("姝e湪瑙e瘑...");
-                        out.write(decryption(buffer), 0, len);
-                    } else {
-                        // 鍗曠函鐨勫鍒�
-                        out.write(buffer, 0, len);
-                    }
-                    size += len;
-                }
-                out.closeEntry();
-                fis.close();
-            }
-            out.close();
-            out.flush();
-            return size;
-        } catch (Exception e) {
-            throw new BaseException(FileCode.LOAD_FTP_PROPERTIES_FAIL.getValue(), FileCode.LOAD_FTP_PROPERTIES_FAIL.getText(), e);
-        }
-    }
+				//鍐欏叆鍘嬬缉鍖�
+				int len;
+				byte[] buffer = new byte[1024];
+				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 {
+						// 鍗曠函鐨勫鍒�
+						out.write(buffer, 0, len);
+					}
+					size += len;
+				}
+				out.closeEntry();
+				fis.close();
+			}
+			out.close();
+			out.flush();
+			return size;
+		} catch (Exception e) {
+			throw new BaseException(FileCode.LOAD_FTP_PROPERTIES_FAIL.getValue(), FileCode.LOAD_FTP_PROPERTIES_FAIL.getText(), e);
+		}
+	}
 
-    /**
-     * 鎷疯礉鏂囦欢
-     *
-     * @param sourceFile
-     * @param aimPath
-     * @param symbol     1锛氬姞瀵嗭紝-1锛氳В瀵嗭紝0锛氫笉鍔�
-     */
-    public static File copyFile(File sourceFile, String aimPath, int symbol) {
-        if (StringUtils.isEmpty(aimPath)) {
-            throw new BaseException(FileCode.INVALID_FILE_PATH.getValue(), FileCode.INVALID_FILE_PATH.getText());
-        }
-        File aimFile = new File(aimPath);
-        File aimDir = aimFile.getParentFile();
-        if (!aimDir.exists()) {
-            aimDir.mkdirs();
-        }
-        try (InputStream is = new FileInputStream(sourceFile);
-             OutputStream os = new FileOutputStream(aimFile);) {
-            int len;
-            byte[] b = new byte[1024];
-            while ((len = is.read(b)) > 0) {
-                if (symbol == 1) {
-                    // 鍔犲瘑
-                    os.write(encryption(b), 0, len);
-                } else if (symbol == -1) {
-                    // 瑙e瘑
-                    os.write(decryption(b), 0, len);
-                } else {
-                    // 鍗曠函鐨勫鍒�
-                    os.write(b, 0, len);
-                }
-            }
-            os.flush();
-            return aimFile;
-        } catch (IOException e) {
-            throw new BaseException(FileCode.COPY_FILE_FAIL.getValue(), FileCode.COPY_FILE_FAIL.getText(), e);
-        }
-    }
+	/**
+	 * 鎷疯礉鏂囦欢
+	 *
+	 * @param sourceFile
+	 * @param aimPath
+	 * @param symbol     1锛氬姞瀵嗭紝-1锛氳В瀵嗭紝0锛氫笉鍔�
+	 */
+	public static File copyFile(File sourceFile, String aimPath, int symbol) {
+		if (StringUtils.isEmpty(aimPath)) {
+			throw new BaseException(FileCode.INVALID_FILE_PATH.getValue(), FileCode.INVALID_FILE_PATH.getText());
+		}
+		File aimFile = new File(aimPath);
+		File aimDir = aimFile.getParentFile();
+		if (!aimDir.exists()) {
+			aimDir.mkdirs();
+		}
+		try (InputStream is = new FileInputStream(sourceFile);
+			 OutputStream os = new FileOutputStream(aimFile);) {
+			int len;
+			byte[] b = new byte[1024];
+			while ((len = is.read(b)) > 0) {
+				if (symbol == 1) {
+					// 鍔犲瘑
+					os.write(encryption(b), 0, len);
+				} else if (symbol == -1) {
+					// 瑙e瘑
+					os.write(decryption(b), 0, len);
+				} else {
+					// 鍗曠函鐨勫鍒�
+					os.write(b, 0, len);
+				}
+			}
+			os.flush();
+			return aimFile;
+		} catch (IOException e) {
+			throw new BaseException(FileCode.COPY_FILE_FAIL.getValue(), FileCode.COPY_FILE_FAIL.getText(), e);
+		}
+	}
 
-    public static void copyFile(File sourceFile, String aimPath) {
-        copyFile(sourceFile, aimPath, 0);
-    }
+	public static void copyFile(File sourceFile, String aimPath) {
+		copyFile(sourceFile, aimPath, 0);
+	}
 
-    /**
-     * 鍔犲瘑
-     *
-     * @param bytes
-     * @return
-     */
-    public static byte[] encryption(byte[] bytes) {
-        /**
-         * 璁剧疆涓�涓姞瀵嗙畻娉曪紝瑕佹眰鍙�嗭紝杩樺彲浠ラ�氳繃瑙e瘑寰楀埌鍘熸潵鐨勪笢瑗�
-         * 鎴戜滑杩欐牱鍋氾細姣忎竴涓瓧鑺傜殑鍊硷紝閮藉姞涓婂畠鐨勭储寮曞彿
-         * 鐒跺悗锛屼氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆锛屽嵆鍔犲瘑缁撴潫
-         */
-        for (int i = 0; i < bytes.length; i++) {
-            bytes[i] = (byte) (bytes[i] + i);
-        }
-        byte temp = bytes[0];
-        bytes[0] = bytes[bytes.length - 1];
-        bytes[bytes.length - 1] = temp;
-        return bytes;
+	/**
+	 * 鍔犲瘑
+	 *
+	 * @param bytes
+	 * @return
+	 */
+	public static byte[] encryption(byte[] bytes) {
+		/**
+		 * 璁剧疆涓�涓姞瀵嗙畻娉曪紝瑕佹眰鍙�嗭紝杩樺彲浠ラ�氳繃瑙e瘑寰楀埌鍘熸潵鐨勪笢瑗�
+		 * 鎴戜滑杩欐牱鍋氾細姣忎竴涓瓧鑺傜殑鍊硷紝閮藉姞涓婂畠鐨勭储寮曞彿
+		 * 鐒跺悗锛屼氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆锛屽嵆鍔犲瘑缁撴潫
+		 */
+		for (int i = 0; i < bytes.length; i++) {
+			bytes[i] = (byte) (bytes[i] + i);
+		}
+		byte temp = bytes[0];
+		bytes[0] = bytes[bytes.length - 1];
+		bytes[bytes.length - 1] = temp;
+		return bytes;
 
-    }
+	}
 
-    /**
-     * 瑙e瘑
-     *
-     * @param bytes
-     * @return
-     */
-    public static byte[] decryption(byte[] bytes) {
-        /**
-         * 瑙e瘑绠楁硶锛屾槸鍔犲瘑鐨勯�嗚繃绋�
-         * 鍏堜氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆
-         * 鐒跺悗锛屾瘡涓�涓瓧鑺傜殑鍊硷紝閮藉噺鍘诲畠鐨勭储寮曞彿
-         */
-        byte temp = bytes[0];
-        bytes[0] = bytes[bytes.length - 1];
-        bytes[bytes.length - 1] = temp;
-        for (int i = 0; i < bytes.length; i++) {
-            bytes[i] = (byte) (bytes[i] - i);
-        }
-        return bytes;
-    }
+	/**
+	 * 瑙e瘑
+	 *
+	 * @param bytes
+	 * @return
+	 */
+	public static byte[] decryption(byte[] bytes) {
+		/**
+		 * 瑙e瘑绠楁硶锛屾槸鍔犲瘑鐨勯�嗚繃绋�
+		 * 鍏堜氦鎹㈢涓�涓瓧鑺傚拰鏈�鍚庝竴涓瓧鑺傜殑浣嶇疆
+		 * 鐒跺悗锛屾瘡涓�涓瓧鑺傜殑鍊硷紝閮藉噺鍘诲畠鐨勭储寮曞彿
+		 */
+		byte temp = bytes[0];
+		bytes[0] = bytes[bytes.length - 1];
+		bytes[bytes.length - 1] = temp;
+		for (int i = 0; i < bytes.length; i++) {
+			bytes[i] = (byte) (bytes[i] - i);
+		}
+		return bytes;
+	}
 }

--
Gitblit v1.9.2