From 42ccf1da495016bf4574f631faad41f586613df9 Mon Sep 17 00:00:00 2001
From: shicf <shi_chongfu@163.com>
Date: 星期三, 15 十月 2025 09:32:18 +0800
Subject: [PATCH] 附件支持存放数据库,读取从数据库获取
---
src/main/java/com/product/file/util/FileUtil.java | 86 ++++++++++++++++++++++--------------------
1 files changed, 45 insertions(+), 41 deletions(-)
diff --git a/src/main/java/com/product/file/util/FileUtil.java b/src/main/java/com/product/file/util/FileUtil.java
index 4e3e573..ea85a09 100644
--- a/src/main/java/com/product/file/util/FileUtil.java
+++ b/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\\鍏充簬鍗忚皟瑙e喅瑗垮畞涓囪揪鍦颁骇缃笟鏈夐檺鍏徃鍟嗗搧鎴块鍞祫閲戠洃绠¤处鎴峰喕缁撶浉鍏抽棶棰� (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鏂囦欢鐨勫ご閮ㄤ篃鏄疍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();
+ }
+
/**
* 鍚堝苟word鏂囨。
*
@@ -41,38 +79,7 @@
* @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;
- }
+ 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) {
@@ -119,7 +127,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);
@@ -144,7 +151,6 @@
fileName = head + "(" + showCount + ")." + tail;
}
fileNameCountMap.put(fileName, ++showCount);
- log.info(String.format("姝e湪鎵撳寘鏂囦欢 %s", fileName));
//浠ヨ鏂囨爣棰樹负姣忎釜鏂囦欢鍛藉悕
FileInputStream fis = new FileInputStream(downloadPath);
@@ -156,11 +162,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