public static Boolean containDirty(String str) throws IOException {
ClassPathResource classPathResource = new ClassPathResource("sensitiveWords");
InputStream inputStream = classPathResource.getInputStream();
String result = IOUtils.toString(inputStream, String.valueOf(StandardCharsets.UTF_8));
String[] dictionary = result.replaceAll(" ", "").split("rn"); //取得关键词词典
if (str != null && !str.equals("")) {
Arrays.sort(dictionary);
int len = str.length();
for (int i = 0; i < len; i++) {
for (int j = len; j > i; j--) {
int rs = Arrays.binarySearch(dictionary, str.substring(i, j));
if (rs >= 0) {
return true;
}
}
}
}
return false;
}
需要在resource 下建立自己的敏感词词库



