Browse Source

从url中提取ossPath

wangyunpeng 6 months ago
parent
commit
bb781ddf58

+ 17 - 13
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/ArticleAuditService.java

@@ -36,6 +36,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
+import java.net.URL;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -126,22 +127,25 @@ public class ArticleAuditService {
     }
 
     private String getOssPath(String ossUrl) {
+        String result = "";
         if (ossUrl == null || ossUrl.isEmpty()) {
-            return "";
+            return result;
         }
-        // 找到 ".com/" 后面的部分
-        String keyword = ".com/";
-        int startIndex = ossUrl.indexOf(keyword);
-        if (startIndex == -1) {
-            return "";
-        }
-        // 提取从 ".com/" 之后的部分,去掉文件后缀名
-        startIndex += keyword.length();
-        int endIndex = ossUrl.lastIndexOf(".");
-        if (endIndex == -1) {
-            endIndex = ossUrl.length();
+        try {
+            URL url = new URL(ossUrl);
+            // 去除域名
+            result = url.getPath();
+            // 获取后缀.位置
+            int endIndex = result.lastIndexOf(".");
+            if (endIndex == -1) {
+                endIndex = result.length();
+            }
+            // 去除 / 及 文件后缀
+            result = result.substring(1, endIndex);
+        } catch (Exception e) {
+            log.error("getOssPath error:{}", e.getMessage());
         }
-        return ossUrl.substring(startIndex, endIndex);
+        return result;
     }
 
     @XxlJob("articleVideoAuditResult")