peng
2025-11-06 c4938f6f4e839890b032c75c7a57333a6a9157a9
backend/src/main/java/com/rongyichuang/judge/service/CosService.java
@@ -124,6 +124,39 @@
    }
    /**
     * 直接上传本地文件到COS
     */
    public String uploadLocalFile(java.io.File file, String fileName) throws Exception {
        // 生成文件路径:按日期分目录
        String dateDir = new java.text.SimpleDateFormat("yyyyMMdd").format(new Date());
        String key = dateDir + "/" + fileName;
        System.out.println("=== COS本地文件上传调试信息 ===");
        System.out.println("文件Key: " + key);
        System.out.println("文件大小: " + file.length());
        System.out.println("文件路径: " + file.getAbsolutePath());
        // 创建COS客户端
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        COSClient cosClient = new COSClient(cred, clientConfig);
        try {
            // 创建上传请求
            com.qcloud.cos.model.PutObjectRequest putObjectRequest =
                new com.qcloud.cos.model.PutObjectRequest(bucket, key, file);
            // 执行上传
            com.qcloud.cos.model.PutObjectResult result = cosClient.putObject(putObjectRequest);
            System.out.println("上传成功,ETag: " + result.getETag());
            return key; // 返回相对路径
        } finally {
            cosClient.shutdown();
        }
    }
    /**
     * 获取文件访问 URL
     */
    public String getFileUrl(String key) {