java工程中过滤敏感词和html标签

敏感词库

资源: https://pan.baidu.com/s/1PKb2BhtVcxiF7cc70qy5Vg 提取码: n4be

敏感词库工具类(注意查看词库编码格式是否为utf-8,否则乱码)

package com.jpxx.pxxdj.common.util;

import java.io.*;
import java.util.*;

/**
 * 敏感词库工具类
 */
public class BadWordUtils {
    public static Set<String> words;
    public static Map<String,String> wordMap;

    /**
     * 最小匹配规则
     */
    public static int minMatchTYpe = 1;

    /**
     * 最大匹配规则
     */
    public static int maxMatchType = 2;

    static{
        InputStream in = ClassLoader.getSystemResourceAsStream("mgck-master/keywords.txt");
        BadWordUtils.words = readTxtByLine(in);
        addBadWordToHashMap(BadWordUtils.words);
    }

    public static Set<String> readTxtByLine(InputStream in){
        Set<String> keyWordSet = new HashSet<>();
        BufferedReader in2=new BufferedReader(new InputStreamReader(in));
        String y="";
        while(true){
            try {
                if (!((y=in2.readLine())!=null)) break;
                keyWordSet.add(y);
                System.out.println(y);
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        return keyWordSet;
    }
    /**
     * 检查文字中是否包含敏感字符,检查规则如下:<br>
     * @param txt
     * @param beginIndex
     * @param matchType
     * @return,如果存在,则返回敏感词字符的长度,不存在返回0
     * @version 1.0
     */
    @SuppressWarnings({ "rawtypes"})
    public static int checkBadWord(String txt,int beginIndex,int matchType){
        boolean  flag = false;
        int matchFlag = 0;
        char word = 0;
        Map nowMap = wordMap;
        for(int i = beginIndex; i < txt.length() ; i++){
            word = txt.charAt(i);
            nowMap = (Map) nowMap.get(word);
            if(nowMap != null){
                matchFlag++;
                if("1".equals(nowMap.get("isEnd"))){
                    flag = true;
                    if(minMatchTYpe == matchType){
                        break;
                    }
                }
            }
            else{
                break;
            }
        }
        if(!flag){
            matchFlag = 0;
        }
        return matchFlag;
    }

    /**
     * 判断文字是否包含敏感字符
     * @param txt  文字
     * @param matchType  匹配规则 1:最小匹配规则,2:最大匹配规则
     * @return 若包含返回true,否则返回false
     * @version 1.0
     */
    public static boolean isContaintBadWord(String txt,int matchType){
        boolean flag = false;
        for(int i = 0 ; i < txt.length() ; i++){
            int matchFlag = checkBadWord(txt, i, matchType);
            if(matchFlag > 0){
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 替换敏感字字符
     * @param txt
     * @param matchType
     * @param replaceChar 替换字符,默认*
     * @version 1.0
     */
    public static String replaceBadWord(String txt,int matchType,String replaceChar){
        String resultTxt = txt;
        Set<String> set = getBadWord(txt, matchType);
        Iterator<String> iterator = set.iterator();
        String word;
        String replaceString;
        while (iterator.hasNext()) {
            word = iterator.next();
            replaceString = getReplaceChars(replaceChar, word.length());
            resultTxt = resultTxt.replaceAll(word, replaceString);
        }

        return resultTxt;
    }
    /**
     * 获取文字中的敏感词
     * @param txt 文字
     * @param matchType 匹配规则 1:最小匹配规则,2:最大匹配规则
     * @return
     * @version 1.0
     */
    public static Set<String> getBadWord(String txt , int matchType){
        Set<String> sensitiveWordList = new HashSet();
        for(int i = 0 ; i < txt.length() ; i++){
            int length = checkBadWord(txt, i, matchType);
            if(length > 0){
                sensitiveWordList.add(txt.substring(i, i+length));
                i = i + length - 1;
            }
        }
        return sensitiveWordList;
    }

    /**
     * 获取替换字符串
     * @param replaceChar
     * @param length
     * @return
     * @version 1.0
     */
    private static String getReplaceChars(String replaceChar,int length){
        String resultReplace = replaceChar;
        for(int i = 1 ; i < length ; i++){
            resultReplace += replaceChar;
        }
        return resultReplace;
    }


    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static void addBadWordToHashMap(Set<String> keyWordSet) {
        wordMap = new HashMap(keyWordSet.size());
        String key;
        Map nowMap;
        Map<String, String> newWorMap;
        Iterator<String> iterator = keyWordSet.iterator();
        while(iterator.hasNext()){
            key = iterator.next();
            nowMap = wordMap;
            for(int i = 0 ; i < key.length() ; i++){
                char keyChar = key.charAt(i);
                Object wordMap = nowMap.get(keyChar);

                if(wordMap != null){
                    nowMap = (Map) wordMap;
                }
                else{
                    newWorMap = new HashMap<>();
                    newWorMap.put("isEnd", "0");
                    nowMap.put(keyChar, newWorMap);
                    nowMap = newWorMap;
                }

                if(i == key.length() - 1){
                    nowMap.put("isEnd", "1");
                }
            }
        }
    }
}

过滤html标签工具类

package com.jpxx.pxxdj.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLUtils {
    /**
     * 过滤所有HTML 标签
     * @param htmlStr
     * @return
     */
    public static String filterHTMLTag(String htmlStr) {
        //定义HTML标签的正则表达式
        String reg_html="<[^>]+>";
        Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        htmlStr=matcher.replaceAll("");
        return htmlStr;
    }

    /**
     * 过滤标签,通过标签名
     * @param htmlStr
     * @param tagName
     * @return
     */
    public static String filterTagByName(String htmlStr,String tagName) {
        String reg_html="<"+tagName+"[^>]*?>[\\s\\S]*?<\\/"+tagName+">";
        Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        htmlStr=matcher.replaceAll(""); //过滤html标签
        return htmlStr;
    }

    /**
     * 过滤标签上的 style 样式
     * @param htmlStr
     * @return
     */
    public static String filterHTMLTagInStyle(String htmlStr) {
        String reg_html="style=('|\")(.*?)('|\")";
        Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        htmlStr=matcher.replaceAll(""); //过滤html标签
        return htmlStr;
    }

    /**
     * 替换表情
     * @param htmlStr
     * @return
     */
    public static String replayFace(String htmlStr) {
        String reg_html="\\[em_\\d{1,}\\]";
        Pattern pattern =Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE);
        Matcher matcher=pattern.matcher(htmlStr);
        if(matcher.find()) {
            matcher.reset();
            while(matcher.find()) {
                String num = matcher.group(0);
                String number=num.substring(num.lastIndexOf('_')+1, num.length()-1);
                htmlStr = htmlStr.replace(num, "<img src='/face/arclist/"+number+".gif' border='0' />");
            }
        }
        return htmlStr;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值