diff --git a/src/main/java/com/zh/project0512/controller/UtilsController.java b/src/main/java/com/zh/project0512/controller/UtilsController.java index 0ae30eb..1b10246 100644 --- a/src/main/java/com/zh/project0512/controller/UtilsController.java +++ b/src/main/java/com/zh/project0512/controller/UtilsController.java @@ -9,6 +9,7 @@ import com.zh.project0512.service.ITaskService; import com.zh.project0512.service.IVideoService; import com.zh.project0512.utils.FileTypeUtil; import com.zh.project0512.utils.RegexUtil; +import com.zh.project0512.utils.RegexUtils; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; import io.swagger.v3.oas.annotations.Operation; @@ -96,9 +97,9 @@ public class UtilsController { commpressPicCycle(filePathSmall, picSizeLimit, 0.8); res.put("fileUrlSmall", "/upload/" + newFileNameSmall); } -// if (!dest.getParentFile().exists()) { -// dest.getParentFile().mkdirs(); -// } + if (!dest.getParentFile().exists()) { + dest.getParentFile().mkdirs(); + } file.transferTo(dest); res.put("fileUrl", "/upload/" + newFileName); @@ -199,11 +200,8 @@ public class UtilsController { } public static void main(String[] args) { - String url = "4.38 wfb:/ 复制打开抖音,看看【酸味真火的作品】坚持总会有收获的# 意想不到的情侣健身 https://v.douyin.com/YT982Sf/"; - Pattern p = Pattern.compile("((https://)+\\S*(/)\\S*(/))", Pattern.CASE_INSENSITIVE); - System.out.println(new RegexUtil().httpReg(url)); -// Matcher m = p.matcher(url); -// System.out.println(m); + String url = "4.38 wfb:/ 复制打开抖音身 https://v.douyin.com/YT98hjf/,看看【酸味真火的作品】坚持总会有收获的# 意想不到的情侣健身 https://v.douyin.com/YT982Sf/"; + System.out.println( RegexUtils.get("(https://)(.*?)(/)(.*?)(/)",url,0)); // String a = m.find() ? m.group(1) : url; // System.out.println(a); // int b = StringUtils.countOccurrencesOf(a, "https://v.douyin"); diff --git a/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java b/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java index 1689cb8..1c59a73 100644 --- a/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java +++ b/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java @@ -12,6 +12,7 @@ import com.zh.project0512.service.IVideoService; import com.zh.project0512.utils.JwtUtil; import com.zh.project0512.utils.MybatisPlusUtil; import com.zh.project0512.utils.RegexUtil; +import com.zh.project0512.utils.RegexUtils; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; import io.jsonwebtoken.Claims; @@ -97,7 +98,7 @@ public class VideoUController { return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "已提交过该平台视频"); } // 校验并提取链接 - String s = new RegexUtil().httpReg(param.getUrl()); + String s = RegexUtils.get("(https://)(.*?)(/)(.*?)(/)",param.getUrl(),0); if(null == s){ return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "视频链接有误"); }else if(param.getType() == 1&&0==StringUtils.countOccurrencesOf(s,"https://v.douyin")){ diff --git a/src/main/java/com/zh/project0512/utils/RegexUtil.java b/src/main/java/com/zh/project0512/utils/RegexUtil.java index 7ab5fe3..68c0804 100644 --- a/src/main/java/com/zh/project0512/utils/RegexUtil.java +++ b/src/main/java/com/zh/project0512/utils/RegexUtil.java @@ -7,7 +7,7 @@ public class RegexUtil { // 截取字符串中https部分 public String httpReg(String url) { - Pattern p = Pattern.compile("((https://)+\\S*\\s)", Pattern.CASE_INSENSITIVE); + Pattern p = Pattern.compile("(https://)(.*?)(/)(.*?)(/)", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(url); String a = m.find() ? m.group(1) : null; return a; diff --git a/src/main/java/com/zh/project0512/utils/RegexUtils.java b/src/main/java/com/zh/project0512/utils/RegexUtils.java new file mode 100644 index 0000000..a323aea --- /dev/null +++ b/src/main/java/com/zh/project0512/utils/RegexUtils.java @@ -0,0 +1,153 @@ +package com.zh.project0512.utils; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * java 正则匹配基本操作工具类 + * 1. 多行匹配 + * 2. 已支持 . (默认正则 不支持) 匹配所有字符包括换行 + *
+ * 参考:https://www.cnblogs.com/Krloypower/p/11356805.html
+ *
+ * @description:
+ * @author: mabh
+ * @create: 2021/5/6 14:09
+ **/
+public final class RegexUtils {
+ /**
+ * 缓存一些 Pattern.compile() 的
+ */
+ private static final Map