Commit eb91ad3e by 徐高华

test

parent 8ed95c5c
...@@ -4,6 +4,8 @@ package com.gic.haoban.manage.web.controller; ...@@ -4,6 +4,8 @@ package com.gic.haoban.manage.web.controller;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -24,8 +26,6 @@ import com.gic.haoban.manage.api.service.MaterialApiService; ...@@ -24,8 +26,6 @@ import com.gic.haoban.manage.api.service.MaterialApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode; import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.utils.EmojiFilterUtil; import com.gic.haoban.manage.web.utils.EmojiFilterUtil;
import cn.hutool.extra.emoji.EmojiUtil;
@RestController @RestController
public class MaterialController extends WebBaseController { public class MaterialController extends WebBaseController {
...@@ -275,14 +275,18 @@ public class MaterialController extends WebBaseController { ...@@ -275,14 +275,18 @@ public class MaterialController extends WebBaseController {
return resultResponse(HaoBanErrCode.ERR_1); return resultResponse(HaoBanErrCode.ERR_1);
} }
public String checkEmoji(String msg) { public static String checkEmoji(String msg) {
if(StringUtils.isBlank(msg)) { if(StringUtils.isBlank(msg)) {
return null ; return null ;
} }
String s = EmojiFilterUtil.filterEmojiLast(msg) ; String s = EmojiFilterUtil.filterEmoji(msg) ;
if(!msg.equals(s)) { if(msg.equals(s)) {
return "保存失败,不支持特殊表情字符" ; return "保存失败,不支持特殊表情字符" ;
} }
return null ; return null ;
} }
public static void main(String[] args) {
System.out.println(EmojiFilterUtil.filterEmoji("aasdsa查$%^&") );;
}
} }
package com.gic.haoban.manage.web.utils; package com.gic.haoban.manage.web.utils;
import com.gic.commons.util.StringUtil;
public class EmojiFilterUtil { public class EmojiFilterUtil {
/**
* 是否包含表情
*/
public static void main(String[] args) { private static boolean isEmojiCharacter(char codePoint) {
return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
System.out.println(filterEmoji("🈶️")); ; || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
} || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
public static String filterEmojiLast(String content) {
byte[] conbyte = content.getBytes();
for (int i = 0; i < conbyte.length; i++) {
if ((conbyte[i] & 0xF8) == 0xF0) {
for (int j = 0; j < 4; j++) {
conbyte[i + j] = 0x30;
}
i += 3;
}
}
content = new String(conbyte);
return filterEmoji(content.replaceAll("0000", ""));
} }
private static String filterEmoji(String source) { public static String filterEmoji(String source) {
if (!containsEmoji(source)) {
return source;// 如果不包含,直接返回
}
// 到这里铁定包含
StringBuilder buf = null; StringBuilder buf = null;
int len = source.length(); int len = source.length();
if (len > 20) {
return "*";
}
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i); char codePoint = source.charAt(i);
if (!isEmojiCharacter(codePoint)) {// 如果不包含 则将字符append
if (isEmojiCharacter(codePoint)) {
if (buf == null) { if (buf == null) {
buf = new StringBuilder(source.length()); buf = new StringBuilder(source.length());
} }
buf.append(codePoint); buf.append(codePoint);
} else { } else {
} }
} }
if (buf == null) { if (buf == null) {
return source;// 如果没有找到 emoji表情,则返回源字符串 return source;// 如果没有找到 emoji表情,则返回源字符串
} else { } else {
...@@ -63,30 +39,4 @@ public class EmojiFilterUtil { ...@@ -63,30 +39,4 @@ public class EmojiFilterUtil {
} }
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
|| (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
// 判断表情符
private static boolean containsEmoji(String source) {
if (StringUtil.isBlank(source)) {
return false;
}
source = source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
// do nothing,判断到了这里表明,确认有表情字符
return true;
}
}
return false;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment