From 92bd9ed78ad1da026b81fcd8e88445b6834bba76 Mon Sep 17 00:00:00 2001
From: zhangherong <571457620@qq.com>
Date: 星期二, 01 四月 2025 17:12:11 +0800
Subject: [PATCH] art: 设备管理-保养标准-点检导入功能

---
 lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java
index 92334c4..10a2842 100644
--- a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java
+++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/FileUtil.java
@@ -1,5 +1,7 @@
 package org.jeecg.common.util;
 
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.jeecg.common.api.vo.FileUploadResult;
 import org.jeecg.common.exception.ExceptionCast;
 import org.springframework.beans.factory.annotation.Value;
@@ -9,10 +11,13 @@
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
+@Slf4j
 @Component
 public class FileUtil {
 
@@ -60,6 +65,42 @@
 		return dto;
 	}
 
+	public static FileUploadResult uploadFile(byte[] fileData, String fileName) {
+		String suffix = getFileSuffix(fileName);
+		Date currentDate = DateUtils.getDate();
+		String monthStr = DateUtils.date2Str(currentDate, DateUtils.monthFormat.get());
+		// 鐩稿璺緞
+		String relativePath = "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/";
+		// 缁濆璺緞
+		String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/";
+		String fileNameNonSuffix = getFilenameNonSuffix(fileName);
+		if (StringUtils.isBlank(fileNameNonSuffix)) {
+			return null;
+		}
+		// 鑾峰彇鏂囦欢鍔犲瘑鍚嶇О 1 淇濊瘉鏂囦欢鍞竴 涓嶅瓨鍦ㄨ鐩栭棶棰� 2 鐩綍涓嬩繚瀛樻枃浠跺姞瀵嗗悕绉� 鍘婚櫎鏂囦欢鍏抽敭淇℃伅
+		String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()) + "." + suffix;
+		Long fileSize = (long) fileData.length;
+        try {
+			File targetFile = new File(absolutePath, encodeFileName);
+			if(!targetFile.getParentFile().exists()) {
+				//鍒涘缓鐩綍
+				targetFile.getParentFile().mkdirs();
+			}
+            Files.write(Paths.get(absolutePath + encodeFileName), fileData);
+			FileUploadResult dto = new FileUploadResult();
+			dto.setFileName(fileNameNonSuffix);
+			dto.setFileEncodeName(encodeFileName);
+			dto.setFilePath(relativePath + encodeFileName);
+			dto.setFileSize(fileSize);
+			dto.setFileSuffix(suffix);
+			return dto;
+        } catch (IOException e) {
+			log.error("鏂囦欢鍐欏叆澶辫触: {}", e.getMessage(), e);
+            return null;
+        }
+
+	}
+
 	/**
 	 * @param filePath 璺緞
 	 * @param fileName 鏂囦欢鍚�

--
Gitblit v1.9.3