From 4cb586238c078799d91e4f22a662aa00fea24eef Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期五, 17 十月 2025 17:27:45 +0800
Subject: [PATCH] 适配存储到数据库的文件; 不需要存储到数据库的,若是集群,那么同步到集群的其他节点; 若是失败,定时任务重试,至多3次
---
src/main/java/com/product/file/util/FileUtil.java | 384 +++++++++++++++++++++++++++++++++---------------------
1 files changed, 234 insertions(+), 150 deletions(-)
diff --git a/src/main/java/com/product/file/util/FileUtil.java b/src/main/java/com/product/file/util/FileUtil.java
index d6f9688..ea85a09 100644
--- a/src/main/java/com/product/file/util/FileUtil.java
+++ b/src/main/java/com/product/file/util/FileUtil.java
@@ -1,16 +1,19 @@
package com.product.file.util;
+import cn.hutool.core.lang.UUID;
+import com.aspose.words.Document;
+import com.aspose.words.SaveFormat;
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 org.apache.log4j.Logger;
+import lombok.extern.slf4j.Slf4j;
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;
@@ -21,167 +24,248 @@
* @Author: 6c
* @Description:
*/
+@Slf4j
public class FileUtil {
- private static Logger logger = Logger.getLogger(FileUtil.class);
- private static Properties properties;
+ private FileUtil() {
+ }
+ public static void main(String[] args) {
+ System.out.println(FileUtil.checkDocType(new File("D:\\downloads\\鍏充簬鍗忚皟瑙e喅瑗垮畞涓囪揪鍦颁骇缃笟鏈夐檺鍏徃鍟嗗搧鎴块鍞祫閲戠洃绠¤处鎴峰喕缁撶浉鍏抽棶棰� (1).doc")));
+ }
- 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);
- /**
- * 鎵归噺鎵撳寘
- *
- * @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);
+ 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";
+ }
+ }
- 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;
+ private static String bytesToHex(byte[] bytes) {
+ StringBuilder hex = new StringBuilder();
+ for (byte b : bytes) {
+ hex.append(String.format("%02X", b));
+ }
+ return hex.toString();
+ }
- //鑾峰緱鏂囦欢鍚�
- 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));
+ /**
+ * 鍚堝苟word鏂囨。
+ *
+ * @param source 婧愭枃浠�
+ * @param target 鐩爣鏂囦欢
+ * @return
+ */
+ public static File mergeFile(File source, File target) throws Exception {
+ return MergeWordDocuments.mergeFile(source, target);
+ }
- //浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕
- FileInputStream fis = new FileInputStream(downloadPath);
- out.putNextEntry(new ZipEntry(fileName));
+ public static File toDocx(File file) {
+ if (!AsposeUtil.getLicense(2)) {
+ return 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);
- //鍐欏叆鍘嬬缉鍖�
- 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);
- }
- }
+ 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) {
+ e.printStackTrace();
+ }
+ return docxFile;
+ }
- /**
- * 鎷疯礉鏂囦欢
- *
- * @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 File toDoc(File file) {
+ if (!AsposeUtil.getLicense(2)) {
+ return file;
+ }
+ String docxName = UUID.randomUUID().toString() + ".doc";
+ 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(), SaveFormat.DOC);
+ return tempFile;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return docxFile;
+ }
- public static void copyFile(File sourceFile, String aimPath) {
- copyFile(sourceFile, aimPath, 0);
- }
+ /**
+ * 鎵归噺鎵撳寘
+ *
+ * @param pathList 鏂囦欢璺緞闆嗗悎
+ * @param os 杈撳嚭娴�
+ * @return zip鏂囦欢淇濆瓨缁濆璺緞
+ */
+ public static long createZip(List<Map<String, String>> pathList, OutputStream os) {
+ try {
+ long size = 0;
+ ZipOutputStream out = new ZipOutputStream(os);
- /**
- * 鍔犲瘑
- *
- * @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;
+ 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);
- /**
- * 瑙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;
- }
+ //浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕
+ 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) {
+ // 鍔犲瘑
+ out.write(encryption(buffer), 0, len);
+ } else if (symbol == -1) {
+ // 瑙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);
+ }
+ }
+
+ 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;
+
+ }
+
+ /**
+ * 瑙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