deason 7 months ago
parent
commit
40cdd0b091

+ 16 - 0
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/FssHelper.java

@@ -6,11 +6,13 @@ import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.xml.bind.DatatypeConverter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.MessageDigest;
+import java.util.Base64;
 
 public class FssHelper {
 
@@ -149,4 +151,18 @@ public class FssHelper {
         }
     }
 
+    public static String md5ToBase64(String md5) {
+        if (StringUtils.isBlank(md5)) {
+            throw new IllegalArgumentException("Invalid md5 parameter");
+        }
+
+        if (md5.length() == 32) {
+            return Base64.getEncoder().encodeToString(DatatypeConverter.parseHexBinary(md5));
+        } else if (md5.length() == 24) {
+            // base64格式
+            return md5;
+        }
+        throw new IllegalArgumentException("Invalid md5 length");
+    }
+
 }

+ 3 - 0
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/impl/AliyunOssService.java

@@ -250,6 +250,7 @@ public class AliyunOssService implements FssService {
         filePath = this.fixOssFilePath(filePath);
         String fileUrl = FssProperty.FSS_URL_PREFIX + "/" + filePath;
 
+        // @see https://help.aliyun.com/zh/oss/developer-reference/include-signatures-in-the-authorization-header
         // 示例:https://{bucket}.oss-cn-shenzhen.aliyuncs.com
         String requestUrl = FssProperty.FSS_ENDPOINT.replace(FssProperty.FSS_REGION_ID,
                 FssProperty.FSS_BUCKET + "." + FssProperty.FSS_REGION_ID);
@@ -275,6 +276,8 @@ public class AliyunOssService implements FssService {
         info.setFormParams(params);
         info.setFormUrl(requestUrl);
         info.setAccessUrl(fileUrl);
+
+        log.info("buildSign filePath:{} md5:{}", filePath, md5);
         return info;
     }
 

+ 2 - 0
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/impl/TencentCosService.java

@@ -234,6 +234,8 @@ public class TencentCosService implements FssService {
         info.setFormParams(params);
         info.setFormUrl(url.toString());
         info.setAccessUrl(fileUrl);
+
+        log.info("buildSign filePath:{} md5:{}", filePath, md5);
         return info;
     }
 

+ 3 - 1
examcloud-support/src/test/java/cn/com/qmth/examcloud/support/test/OssClientTest.java

@@ -83,7 +83,9 @@ public class OssClientTest {
              ResponseBody body = response.body();) {
             String bodyStr = body != null ? body.string() : null;
             if (response.isSuccessful()) {
-                System.out.println("上传成功!" + bodyStr);
+                String reqMD5 = FssHelper.md5ToBase64(testFileMd5);
+                String respMD5 = response.header("Content-MD5");
+                System.out.println("上传成功!reqMD5:" + reqMD5 + " respMD5:" + respMD5);
                 return;
             }
             System.out.println("上传失败!" + bodyStr);