“djh”
2026-02-05 868cf9aef166be2cba108bcc1fa5469195776673
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 {
@@ -444,5 +536,6 @@
        return uploadObjectVO;
    }
}