zhangjinli 3 years ago
parent 5b26481f82
commit 135ffd2fc2

@ -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");

@ -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")){

@ -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;

@ -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. . ( )
* <p>
* :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<String, Pattern> PATTERN = new HashMap<>();
private RegexUtils() {
}
public static String get(String regex, String content) {
return get(regex, content, 0);
}
public static String get(String regex, String content, int groupIndex) {
return get(getPattern(regex), content, groupIndex);
}
public static String get(Pattern pattern, String content) {
return get(pattern, content, 0);
}
public static String get(Pattern pattern, String content, int groupIndex) {
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
return matcher.group(groupIndex);
}
return null;
}
/**
*
*
* @param regex
* @param input
* @return list
* @see #getMatcherList(String, CharSequence, int)
*/
public static List<String> getMatcherList(String regex, CharSequence input) {
return getMatcherList(regex, input, 0);
}
/**
*
*
* @param regex
* @param input
* @param group //group(0) 返回符合条件的字符串, group(1) 表示正则表达式中符合条件的字符串中的第一个() 中的字符串。
* @return list
* @see #getMatcherList(String, CharSequence)
*/
public static List<String> getMatcherList(String regex, CharSequence input, int group) {
List<String> list = new ArrayList<>();
forEach(regex, input, matcher -> list.add(matcher.group(group)));
return list;
}
/**
*
*
* @param regex
* @param input
* @param consumer
* @see #getMatcherList(String, CharSequence, int)
* @see #findAny(String, CharSequence)
*/
public static void forEach(String regex, CharSequence input, Consumer<Matcher> consumer) {
Matcher matcher = getMatcher(getPattern(regex), input);
while (matcher.find()) {
consumer.accept(matcher);
}
}
/**
*
*
* @param regex
* @param input
* @return true
*/
public static boolean findAny(String regex, CharSequence input) {
//默认贪婪是匹配,如果仅仅查询是否有匹配项,直接是否非贪婪即可,性能更好,哪怕这种性能微乎其微
return findAny(getPattern("^" + regex), input);
}
public static boolean findAny(Pattern pattern, CharSequence input) {
return getMatcher(pattern, input).find();
}
/**
*
*
* @param regex
* @param input
* @return
*/
public static boolean matches(String regex, CharSequence input) {
return getMatcher(getPattern(regex), input).matches();
}
/**
*
*
* @param regex
* @return
*/
private static Pattern getPattern(String regex) {
Pattern pattern = PATTERN.getOrDefault(regex, Pattern.compile(regex,Pattern.DOTALL | Pattern.MULTILINE));
PATTERN.put(regex, pattern);
return pattern;
}
/**
* ,
*
* @param pattern
* @param input
* @return
*/
private static Matcher getMatcher(Pattern pattern, CharSequence input) {
if (pattern == null) {
return null;
}
String regex = pattern.pattern();
if (!PATTERN.containsKey(regex)) {
PATTERN.put(regex, pattern);
}
return pattern.matcher(input);
}
}
Loading…
Cancel
Save