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/service/FileManagerService.java | 290 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 255 insertions(+), 35 deletions(-)
diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java
index cd6e528..a1b32dd 100644
--- a/src/main/java/com/product/file/service/FileManagerService.java
+++ b/src/main/java/com/product/file/service/FileManagerService.java
@@ -1,9 +1,11 @@
package com.product.file.service;
+import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.product.common.lang.StringUtils;
+import com.product.core.config.CoreConst;
import com.product.core.config.Global;
import com.product.core.connection.ConnectionManager;
import com.product.core.dao.BaseDao;
@@ -16,22 +18,23 @@
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.file.config.CmnConst;
import com.product.file.config.FileCode;
+import com.product.file.util.AsposeUtil;
import com.product.file.util.CreateDocumentIndexThread;
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;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletRequest;
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.*;
@@ -52,6 +55,15 @@
public BaseDao baseDao;
@Autowired
FileUtils fileUtils;
+
+ /**
+ * 鏍囪鏂囦欢宸茶淇敼
+ * @param uuid
+ * @param userId
+ */
+ public void signUpdateAttachment(String uuid, int userId) {
+ baseDao.executeUpdate("UPDATE product_sys_attachments SET updated_by = ?, updated_utc_datetime = NOW() WHERE uuid = ?", new Object[] {userId, uuid});
+ }
/**
* 鑾峰彇闈欐�佽祫婧�
@@ -321,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;
}
// 宸茬粡鍒ゅ畾涓洪檮浠讹紝杩涜鎿嶄綔
@@ -353,8 +365,14 @@
}
}
+ boolean saveInDbFlag = FieldType.FILE_ATTACHMENT.getDictValue().equals(fieldFse.getString("field_type"));
+ String fileFinalName = "";
+ if (!saveInDbFlag) {
+ fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid);
+ }
- final String fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid);
+ ///璇诲彇闄勪欢鏃讹紝鏍规嵁瀛楁绫诲瀷FileAttachment鍒ゆ柇鏄粠鐩綍涓繕鏄暟鎹簱涓幏鍙栨枃浠�
+
// 璁板綍闄勪欢淇℃伅鍒版暟鎹簱
logger.info("姝e湪璁板綍闄勪欢淇℃伅鍒版暟鎹簱...");
@@ -370,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);
@@ -416,6 +444,8 @@
logger.info("姝e湪鍥炲啓uuid...");
System.out.println(uuids);
fse.setValue(fieldName, uuids);
+
+ // todo 闆嗙兢甯冪讲鏃讹紝闄勪欢淇℃伅瑕佸悓姝ュ埌鍏跺畠鏈嶅姟鍣ㄤ笂
}
return fse;
}
@@ -449,6 +479,146 @@
}
/**
+ * 绉诲姩绔崌绾э紝瀹夎鍖呬笅杞�
+ *
+ * @param fse
+ * @param response
+ */
+ public void getFileContent(FieldSetEntity fse, HttpServletResponse response, boolean isUpgrade) throws IOException {
+ if (isUpgrade) {
+ FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, fse.getUUID(), false);
+ if (attachmentFse == null || !"product_sys_app_version".equals(attachmentFse.getString("attachment_data_table"))) {
+ return;
+ }
+ logger.info("姝e湪鑾峰彇鏂囦欢娴�...");
+ boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN));
+ String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL);
+ String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE);
+ //鐪熷疄鏂囦欢鍚�
+ String realFileName = attachmentFse.getString(CmnConst.FILE_NAME);
+ String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN);
+ boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN));
+ boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign);
+
+ String clientType = CoreConst.CLIENT_TYPE_APP;
+
+ String contentType = "multipart/form-data";
+ boolean isExcel = realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls");
+ if (needOnlineViewFlag) {
+ //鐗规畩澶勭悊锛� 濡傛灉瀹㈡埛绔笉鏄疉pp 浣嗛瑙堢殑鏂囦欢鏄疎xcel 鐩存帴杩斿洖excel婧愭枃浠� content澶存爣璇嗘槸excel cheng 2025骞�2鏈�11鏃�10:41:41
+ if (isExcel && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+ contentType = "application/vnd.ms-excel";
+ } else {
+ dir += File.separator + CmnConst.TRANSFER_DIR_NAME;
+ }
+ }
+
+ String path = dir + File.separator + fileName;
+ if (needDownloadFromServerFlag) {
+ // 闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�
+ FTPService ftpService = new FTPService();
+ logger.info("闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�...");
+ response.setHeader("Access-Control-Expose-Headers", "*");
+
+ response.setContentType(contentType);
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
+ //璁剧疆澶翠腑鐨勬枃浠剁被鍨�
+ if (!needOnlineViewFlag && !encrptSignFlag) {
+ logger.info("涓嶉渶瑕佽В瀵嗕笖鑾峰彇鍘熸枃浠�...");
+ try (ServletOutputStream os = response.getOutputStream()) {
+ ftpService.downloadFile(path, os);
+ }
+ } else {
+ logger.info("闇�瑕佽В瀵嗘垨鑰呰幏鍙栬浆鎹㈠悗鐨勬枃浠�...");
+ String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName;
+ File localTempFile = new File(localTempPath);
+ OutputStream tempOs = new FileOutputStream(localTempFile);
+ ftpService.downloadFile(path, tempOs);
+ tempOs.flush();
+ tempOs.close();
+ if (needOnlineViewFlag && localTempFile.length() == 0L) {
+ // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣�
+ File sourceFile = fileUtils.getFile(true, 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, true, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME));
+ tempSourceFile.delete();
+
+ // 閲嶆柊鎷夋枃浠�
+ tempOs = new FileOutputStream(localTempFile);
+ ftpService = new FTPService();
+ ftpService.downloadFile(path, tempOs);
+ tempOs.flush();
+ tempOs.close();
+ }
+ response.setContentLengthLong(localTempFile.length());
+ InputStream is = new FileInputStream(localTempFile);
+ int len;
+ byte[] b = new byte[1024];
+ try (ServletOutputStream os = response.getOutputStream()) {
+ while ((len = is.read(b)) > 0) {
+ os.write(encrptSignFlag ? FileUtil.decryption(b) : b, 0, len);
+ }
+ os.flush();
+ is.close();
+ }
+ // 鍒犻櫎涓存椂鏂囦欢
+ 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();
+ }
+ 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());
+ try (ServletOutputStream os = response.getOutputStream()) {
+ while ((len = is.read(b)) > 0) {
+ if (encrptSignFlag) {
+ // 闇�瑕佽В瀵�
+ logger.info("闇�瑕佽В瀵�...");
+ os.write(FileUtil.decryption(b), 0, len);
+ } else {
+ // 鏃犻渶瑙e瘑
+ os.write(b, 0, len);
+ }
+ }
+ os.flush();
+ }
+ is.close();
+ }
+ logger.info("鏂囦欢娴佽幏鍙栨垚鍔�");
+ }
+
+ }
+
+ /**
* 鎻愬彇鏂囦欢淇℃伅-涓嬭浇鏂囦欢鎴栬�呭湪绾块瑙堟枃浠�
*
* @param fse
@@ -469,9 +639,17 @@
String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN);
boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN));
boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign);
- if (needOnlineViewFlag && !realFileName.endsWith(".xlsx") && !realFileName.endsWith(".xls")) {
- // 闇�瑕佸湪绾块瑙堜笖杞崲涔嬪悗鎵嶈兘鍦ㄧ嚎棰勮 excel 鏂囦欢涓嶉渶瑕佽浆鎹㈢洿鎺ヨ緭鍑洪瑙�
- dir += File.separator + CmnConst.TRANSFER_DIR_NAME;
+ String clientType = SpringMVCContextHolder.getCurrentUser().getClientType();
+
+ String contentType = "multipart/form-data";
+ boolean isExcel = realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls");
+ if (needOnlineViewFlag) {
+ //鐗规畩澶勭悊锛� 濡傛灉瀹㈡埛绔笉鏄疉pp 浣嗛瑙堢殑鏂囦欢鏄疎xcel 鐩存帴杩斿洖excel婧愭枃浠� content澶存爣璇嗘槸excel cheng 2025骞�2鏈�11鏃�10:41:41
+ if (isExcel && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+ contentType = "application/vnd.ms-excel";
+ } else {
+ dir += File.separator + CmnConst.TRANSFER_DIR_NAME;
+ }
}
String path = dir + File.separator + fileName;
@@ -480,11 +658,8 @@
FTPService ftpService = new FTPService();
logger.info("闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�...");
response.setHeader("Access-Control-Expose-Headers", "*");
- if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) {
- response.setContentType("application/vnd.ms-excel");
- } else {
- response.setContentType("multipart/form-data");
- }
+
+ response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
//璁剧疆澶翠腑鐨勬枃浠剁被鍨�
if (!needOnlineViewFlag && !encrptSignFlag) {
@@ -515,6 +690,19 @@
tempOs.flush();
tempOs.close();
}
+ boolean delete = false;
+ if (realFileName.endsWith(".xls") && needOnlineViewFlag && localTempFile.exists() && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+ //杞崲涓簒lsx
+ AsposeUtil.xls2xlsx(localTempFile.getPath(), getSystemConfig("temp.dir", "/attachment/temp") + "/" + localTempFile.getName(), "xls");
+ File oldFile = localTempFile;
+ localTempFile = new File(getSystemConfig("temp.dir", "/attachment/temp") + "/" + localTempFile.getName());
+ if (!localTempFile.isFile()) {
+ localTempFile = oldFile;
+ } else {
+ delete = true;
+ oldFile.delete();
+ }
+ }
response.setContentLengthLong(localTempFile.length());
InputStream is = new FileInputStream(localTempFile);
int len;
@@ -530,30 +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()) {
- // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣�
- 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();
- }
+ 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", "*");
- if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) {
- response.setContentType("application/vnd.ms-excel");
- } else {
- response.setContentType("multipart/form-data");
- }
+
+ 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) {
@@ -568,6 +785,9 @@
os.flush();
}
is.close();
+ if (delete && file != null) {
+ file.delete();
+ }
}
logger.info("鏂囦欢娴佽幏鍙栨垚鍔�");
}
--
Gitblit v1.9.2