From ad1e42f88869b109a6273607e740cf8593e832c1 Mon Sep 17 00:00:00 2001
From: heheng <475597332@qq.com>
Date: Thu, 05 Feb 2026 09:00:42 +0800
Subject: [PATCH] 附件复制
---
multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java | 86 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 86 insertions(+), 0 deletions(-)
diff --git a/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java b/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java
index f1b227c..d3f79cc 100644
--- a/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java
+++ b/multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java
@@ -39,6 +39,7 @@
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.concurrent.CountDownLatch;
@@ -74,6 +75,86 @@
private ThreadPoolTaskExecutor poolTaskExecutor;
@Override
+ public String copyFileWithCompanyId(String sourceFilePath, Long companyId) {
+ // 1. 解析原始文件路径
+ File sourceFile = new File(sourceFilePath);
+ if (!sourceFile.exists() || !sourceFile.isFile()) {
+ throw new ApiException("源文件不存在或不是一个有效文件");
+ }
+
+ // 2. 获取原始文件名和扩展名
+ String originalFileName = sourceFile.getName();
+ String fileExtension = "";
+ int dotIndex = originalFileName.lastIndexOf(".");
+ if (dotIndex > 0) {
+ fileExtension = originalFileName.substring(dotIndex); // 包含点号
+ }
+
+ // 3. 生成新文件名(使用公司ID + 扩展名)
+ String newFileName = UUID.randomUUID().toString().replace("-","")+"_"+companyId + fileExtension;
+ String dateStr= DateUtil.format(new Date(),"yyyyMMdd");
+ // 4. 构造目标文件路径
+ String targetDirPath = System.getProperty("user.dir") + File.separator + uploadPath+File.separator+dateStr;
+ File targetDir = new File(targetDirPath);
+ if (!targetDir.exists()) {
+ targetDir.mkdirs(); // 创建目录
+ }
+ String targetFilePath = targetDirPath + File.separator + newFileName;
+
+ // 5. 复制文件
+ try {
+ FileUtils.copyFile(sourceFile, new File(targetFilePath));
+ //Files.copy(sourceFile, new File(targetFilePath), StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ log.error("文件复制失败: {}", e.getMessage());
+ throw new ApiException("文件复制失败,请联系管理员");
+ }
+ // 6. 返回新文件路径
+ return targetFilePath.replace("\\", "/");
+
+ }
+
+ public static String copyFileWithCompanyIdA(String sourceFilePath, Long companyId) {
+ // 1. 解析原始文件路径
+ File sourceFile = new File(sourceFilePath);
+ if (!sourceFile.exists() || !sourceFile.isFile()) {
+ throw new ApiException("源文件不存在或不是一个有效文件");
+ }
+
+ // 2. 获取原始文件名和扩展名
+ String originalFileName = sourceFile.getName();
+ String fileExtension = "";
+ int dotIndex = originalFileName.lastIndexOf(".");
+ if (dotIndex > 0) {
+ fileExtension = originalFileName.substring(dotIndex); // 包含点号
+ }
+
+ // 3. 生成新文件名(使用公司ID + 扩展名)
+ String newFileName = UUID.randomUUID().toString().replace("-","")+"_"+companyId + fileExtension;
+ String dateStr= DateUtil.format(new Date(),"yyyyMMdd");
+ // 4. 构造目标文件路径
+ String targetDirPath = System.getProperty("user.dir") + File.separator + "images"+File.separator+dateStr;
+ File targetDir = new File(targetDirPath);
+ if (!targetDir.exists()) {
+ targetDir.mkdirs(); // 创建目录
+ }
+ String targetFilePath = targetDirPath + File.separator + newFileName;
+
+ // 5. 复制文件
+ try {
+ FileUtils.copyFile(sourceFile, new File(targetFilePath));
+ //Files.copy(sourceFile, new File(targetFilePath), StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ log.error("文件复制失败: {}", e.getMessage());
+ throw new ApiException("文件复制失败,请联系管理员");
+ }
+ // 6. 返回新文件路径
+ return targetFilePath.replace("\\", "/");
+
+ }
+
+
+ @Override
public UploadObjectVO uploadFile(MultipartFile file) {
if(file==null){
throw new ApiException("上传文件不能为空");
@@ -96,6 +177,10 @@
dirFile.delete();
return true;
}
+
+
+
+
@Override
public String uploadVideo2M3u8(MultipartFile file) throws Exception {
@@ -444,5 +529,6 @@
return uploadObjectVO;
}
+
}
--
Gitblit v1.9.2