From c26e227abe288476c11b0a8b7875045e71efa14c Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: Thu, 30 Apr 2026 17:30:00 +0800
Subject: [PATCH] 新增修改

---
 multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java |   95 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 94 insertions(+), 1 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 9d8f52d..479bd49 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,93 @@
     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("文件复制失败,请联系管理员");
+        }
+
+        String standardizedUploadPath = uploadPath.endsWith("/") || uploadPath.endsWith("\\")
+                ? uploadPath.substring(0, uploadPath.length() - 1)
+                : uploadPath;
+        // 6.2 拼接相对路径(统一用/分隔,格式:uploadPath/dateStr/newFileName)
+        String relativeFilePath = standardizedUploadPath + "/" + dateStr + "/" + newFileName;
+
+        // 7. 返回相对路径(替换原来的绝对路径)
+        return relativeFilePath;
+
+    }
+
+    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 +184,10 @@
         dirFile.delete();
         return true;
     }
+
+
+
+
 
     @Override
     public String uploadVideo2M3u8(MultipartFile file) throws Exception {
@@ -262,7 +354,7 @@
     @Override
     @Transactional(rollbackFor = RuntimeException.class)
     public void importStudent() {
-        String path="/home/java/train_exam/back/安全教育学员模板.xlsx";
+        String path="/home/java/multi_system/back/安全教育学员模板.xlsx";
       //  String path="F:/kzy/乱七八糟/安全教育学员模板.xlsx";
         List<StudentExcelData> studentExcelDataList=EasyExcel.read(path, StudentExcelData.class,new StudentExcelDataListener()).sheet().doReadSync();
         List<ExStudent> students=new ArrayList<>();
@@ -444,5 +536,6 @@
         return uploadObjectVO;
     }
 
+
 }
 

--
Gitblit v1.9.2