From 1272dcd48d710491afcc589f5beb2bb22be5334c Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期三, 15 十月 2025 17:41:06 +0800
Subject: [PATCH] 适配存储到数据库的文件
---
src/main/java/com/product/file/config/CmnConst.java | 1
src/main/java/com/product/file/service/FileManagerService.java | 127 +++++++++++++++++++++++++-----------------
2 files changed, 76 insertions(+), 52 deletions(-)
diff --git a/src/main/java/com/product/file/config/CmnConst.java b/src/main/java/com/product/file/config/CmnConst.java
index fc04f96..7f99251 100644
--- a/src/main/java/com/product/file/config/CmnConst.java
+++ b/src/main/java/com/product/file/config/CmnConst.java
@@ -74,6 +74,7 @@
public static final String REAL_FILE_NAME = "realFileName";
public static final String NEED_LOCAL_TEMP_FILE_SIGN = "needLocalTempFileSign";
public static final String SOURCE_PATH = "sourcePath";
+ public static final String FILE_CONTENT = "file_content";
//瀛楁甯搁噺瀹氫箟
//mrbase_sys_directory鏂囦欢鐩綍琛�
diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java
index 2e15c93..a1b32dd 100644
--- a/src/main/java/com/product/file/service/FileManagerService.java
+++ b/src/main/java/com/product/file/service/FileManagerService.java
@@ -23,6 +23,7 @@
import com.product.file.util.FileUtil;
import com.product.file.util.FileUtils;
import com.product.module.sys.entity.SystemUser;
+import com.product.tool.table.enums.FieldType;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -31,6 +32,9 @@
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.sql.Blob;
+import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -51,7 +55,7 @@
public BaseDao baseDao;
@Autowired
FileUtils fileUtils;
-
+
/**
* 鏍囪鏂囦欢宸茶淇敼
* @param uuid
@@ -329,8 +333,8 @@
String uuids = "";
for (Map.Entry<Object, Object> entry : map.entrySet()) {
fieldFse = fieldMetaEntity.getFieldMeta(fieldName);
- if (fieldFse == null || (!CmnConst.ATTACHMENT_TYPE.equals(fieldFse.getString(CmnConst.FIELD_TYPE)) && !"file-image".equals(fieldFse.getString(CmnConst.FIELD_TYPE)))) {
- fse.setValue(entry.getKey().toString(), null);
+ if (fieldFse == null || !Arrays.asList(CmnConst.ATTACHMENT_TYPE, "file-image", FieldType.FILE_ATTACHMENT.getDictValue()).contains(fieldFse.getString(CmnConst.FIELD_TYPE))) {
+ fse.setValue(entry.getKey().toString(), null);
continue;
}
// 宸茬粡鍒ゅ畾涓洪檮浠讹紝杩涜鎿嶄綔
@@ -361,17 +365,15 @@
}
}
-
- final String fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid);
- ///鍒ゆ柇闄勪欢瀛楁鐨勭被鍨嬩负FileAttachment鏃讹紝瑕佹妸鏂囨。鍐呭鏀惧埌闄勪欢琛ㄤ腑鐨刦ile_content瀛楁涓�
-
+ boolean saveInDbFlag = FieldType.FILE_ATTACHMENT.getDictValue().equals(fieldFse.getString("field_type"));
+ String fileFinalName = "";
+ if (!saveInDbFlag) {
+ fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid);
+ }
+
///璇诲彇闄勪欢鏃讹紝鏍规嵁瀛楁绫诲瀷FileAttachment鍒ゆ柇鏄粠鐩綍涓繕鏄暟鎹簱涓幏鍙栨枃浠�
-
- //寰呮敼......
- //闆嗙兢甯冪讲鏃讹紝闄勪欢淇℃伅瑕佸悓姝ュ埌鍏跺畠鏈嶅姟鍣ㄤ笂
- //寰呮敼
-
-
+
+
// 璁板綍闄勪欢淇℃伅鍒版暟鎹簱
logger.info("姝e湪璁板綍闄勪欢淇℃伅鍒版暟鎹簱...");
FieldSetEntity dictFse = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DICT,
@@ -386,6 +388,16 @@
fileType = tail;
attachmentFse.setValue(CmnConst.ATTACHMENT_TYPE_UUID, tail);
}
+ if (saveInDbFlag) {
+ // 鍒ゆ柇闄勪欢瀛楁鐨勭被鍨嬩负FileAttachment鏃讹紝瑕佹妸鏂囨。鍐呭鏀惧埌闄勪欢琛ㄤ腑鐨刦ile_content瀛楁涓�
+ try {
+ // todo 6c
+// attachmentFse.setValue(CmnConst.FILE_CONTENT, new FileInputStream(tempFile));
+ attachmentFse.setValue(CmnConst.FILE_CONTENT, com.product.common.io.FileUtils.readFileToByteArray(tempFile));
+ } catch (Exception e) {
+ throw new RuntimeException("鑾峰彇鏂囦欢娴佸け璐ワ細666");
+ }
+ }
//鍏佽缂栬緫
int allowEdit = ("," + Global.getSystemConfig("can.web.online.edit", "doc,docx,xls,xlsx.ppt,pptx,wps,cvs,wps,wpt,et,eet") + ",").indexOf("," + fileType + ",") != -1 ? 1 : 0;
attachmentFse.setValue(CmnConst.OPT_FLAT, 2);
@@ -432,6 +444,8 @@
logger.info("姝e湪鍥炲啓uuid...");
System.out.println(uuids);
fse.setValue(fieldName, uuids);
+
+ // todo 闆嗙兢甯冪讲鏃讹紝闄勪欢淇℃伅瑕佸悓姝ュ埌鍏跺畠鏈嶅姟鍣ㄤ笂
}
return fse;
}
@@ -704,50 +718,59 @@
localTempFile.delete();
}
} else {
- // 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢
- logger.info("鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢...");
- String localBasePath = Global.getSystemConfig("local.dir", "");
- path = localBasePath + File.separator + path;
- File file = new File(path);
- if (needOnlineViewFlag && file.exists() && CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
- //鐗规畩澶勭悊锛� 鏄疉pp 浣嗛瑙堢殑鏂囦欢鏄疎xcel 闇�瑕佸皢涔嬪墠宸茶浆鎹负鐨勬枃浠跺垹闄ゆ帀閲嶆柊杞崲锛堝洜杞崲鍚庣殑鏍煎紡涓嶆槸pdf)
- String changeTime = "2025-02-11 23:59:59";
- Date changeDate = DateUtil.parse(changeTime, "yyyy-MM-dd HH:mm:ss");
- long fileTime = file.lastModified();
- //濡傛灉鏃堕棿鏄� 2025骞�2鏈�10鏃�23:59:59锛岄偅涔堝氨閲嶆柊杞崲
- //鍒犻櫎鏂囦欢
- if (fileTime <= changeDate.getTime()) {
- file.delete();
- }
- }
- if (needOnlineViewFlag && !file.exists()) {
- // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣�
- File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag);
- File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME));
- sourceFile.renameTo(tempSourceFile);
- FileUtils.convertPdf(false, false, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME));
- tempSourceFile.delete();
- }
- boolean delete = false;
- if (realFileName.endsWith(".xls") && needOnlineViewFlag && file.exists() && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
- //杞崲涓簒lsx
- AsposeUtil.xls2xlsx(file.getPath(), getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName(), "xls");
- File oldFile = file;
- file = new File(getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName());
- if (!file.isFile()) {
- file = oldFile;
- } else {
- delete = true;
- }
- }
+ InputStream is;
+ boolean delete = false;
+ File file = null;
+ long fileLength = 0;
+ if (StringUtils.isEmpty(attachmentFse.getString("attachment_title"))) {
+ // todo 6c
+ is = new ByteArrayInputStream((byte[]) attachmentFse.getValue("file_content"));
+ } else {
+ // 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢
+ logger.info("鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢...");
+ String localBasePath = Global.getSystemConfig("local.dir", "");
+ path = localBasePath + File.separator + path;
+ file = new File(path);
+ if (needOnlineViewFlag && file.exists() && CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+ //鐗规畩澶勭悊锛� 鏄疉pp 浣嗛瑙堢殑鏂囦欢鏄疎xcel 闇�瑕佸皢涔嬪墠宸茶浆鎹负鐨勬枃浠跺垹闄ゆ帀閲嶆柊杞崲锛堝洜杞崲鍚庣殑鏍煎紡涓嶆槸pdf)
+ String changeTime = "2025-02-11 23:59:59";
+ Date changeDate = DateUtil.parse(changeTime, "yyyy-MM-dd HH:mm:ss");
+ long fileTime = file.lastModified();
+ //濡傛灉鏃堕棿鏄� 2025骞�2鏈�10鏃�23:59:59锛岄偅涔堝氨閲嶆柊杞崲
+ //鍒犻櫎鏂囦欢
+ if (fileTime <= changeDate.getTime()) {
+ file.delete();
+ }
+ }
+ if (needOnlineViewFlag && !file.exists()) {
+ // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣�
+ File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag);
+ File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME));
+ sourceFile.renameTo(tempSourceFile);
+ FileUtils.convertPdf(false, false, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME));
+ tempSourceFile.delete();
+ }
+ if (realFileName.endsWith(".xls") && needOnlineViewFlag && file.exists() && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+ //杞崲涓簒lsx
+ AsposeUtil.xls2xlsx(file.getPath(), getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName(), "xls");
+ File oldFile = file;
+ file = new File(getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName());
+ if (!file.isFile()) {
+ file = oldFile;
+ } else {
+ delete = true;
+ }
+ }
+ is = Files.newInputStream(file.toPath());
+ fileLength = file.length();
+ }
int len;
byte[] b = new byte[1024];
- InputStream is = new FileInputStream(file);
response.setHeader("Access-Control-Expose-Headers", "*");
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
- response.setContentLengthLong(file.length());
+ response.setContentLengthLong(fileLength);
try (ServletOutputStream os = response.getOutputStream()) {
while ((len = is.read(b)) > 0) {
if (encrptSignFlag) {
@@ -762,7 +785,7 @@
os.flush();
}
is.close();
- if (delete) {
+ if (delete && file != null) {
file.delete();
}
}
--
Gitblit v1.9.2