Commit 2e57175e by 徐高华

特殊字符

parent 488ba86c
package com.gic.haoban.manage.web.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.base.api.common.ServiceResponse;
......@@ -12,15 +22,9 @@ import com.gic.haoban.manage.api.dto.MaterialCategoryDTO;
import com.gic.haoban.manage.api.dto.MaterialDTO;
import com.gic.haoban.manage.api.service.MaterialApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gic.haoban.manage.web.utils.EmojiFilterUtil;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import cn.hutool.extra.emoji.EmojiUtil;
@RestController
public class MaterialController extends WebBaseController {
......@@ -126,6 +130,10 @@ public class MaterialController extends WebBaseController {
materialDTO.setStaffId(login.getClerkId());
materialDTO.setStaffName(login.getClerkName());
String msg = this.checkEmoji(materialDTO.getMaterialContent()) ;
if(null != msg) {
return this.fail(msg) ;
}
String materialTitle = materialDTO.getMaterialTitle();
String categoryId = materialDTO.getCategoryId();
Integer categoryType = materialDTO.getMaterialType();
......@@ -185,6 +193,10 @@ public class MaterialController extends WebBaseController {
materialDTO.setStaffId(login.getClerkId());
materialDTO.setStaffName(login.getClerkName());
String materialId = materialDTO.getMaterialId();
String msg = this.checkEmoji(materialDTO.getMaterialContent()) ;
if(null != msg) {
return this.fail(msg) ;
}
MaterialDTO dto = materialApiService.selectMaterialById(materialId);
if (dto == null) {
return resultResponse(HaoBanErrCode.ERR_2);
......@@ -262,5 +274,14 @@ public class MaterialController extends WebBaseController {
}
return resultResponse(HaoBanErrCode.ERR_1);
}
public String checkEmoji(String msg) {
if(StringUtils.isBlank(msg)) {
return null ;
}
if(EmojiFilterUtil.containsEmoji(msg)) {
return "保存失败,不支持特殊表情字符" ;
}
return null ;
}
}
package com.gic.haoban.manage.web.utils;
import com.gic.commons.util.StringUtil;
public class EmojiFilterUtil {
public static void main(String[] args) {
System.out.println(filterEmoji("🈶️")); ;
}
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) {
if (!containsEmoji(source)) {
return source;// 如果不包含,直接返回
}
// 到这里铁定包含
StringBuilder buf = null;
int len = source.length();
if (len > 20) {
return "*";
}
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
} else {
}
}
if (buf == null) {
return source;// 如果没有找到 emoji表情,则返回源字符串
} else {
if (buf.length() == len) {// 这里的意义在于尽可能少的toString,因为会重新生成字符串
buf = null;
return source;
} else {
return buf.toString();
}
}
}
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));
}
// 判断表情符
public 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