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 |  117 +++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 74 insertions(+), 43 deletions(-)

diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java
index 144a958..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,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湪璁板綍闄勪欢淇℃伅鍒版暟鎹簱...");
@@ -378,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);
@@ -424,6 +444,8 @@
 			logger.info("姝e湪鍥炲啓uuid...");
 			System.out.println(uuids);
 			fse.setValue(fieldName, uuids);
+
+            // todo 闆嗙兢甯冪讲鏃讹紝闄勪欢淇℃伅瑕佸悓姝ュ埌鍏跺畠鏈嶅姟鍣ㄤ笂
 		}
 		return fse;
 	}
@@ -696,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) {
@@ -754,7 +785,7 @@
 				os.flush();
 			}
 			is.close();
-			if (delete) {
+			if (delete && file != null) {
 				file.delete();
 			}
 		}

--
Gitblit v1.9.2