package com.kidgrow.common.utils;
|
|
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.StringEscapeUtils;
|
|
import java.io.PrintWriter;
|
import java.io.StringWriter;
|
import java.io.UnsupportedEncodingException;
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
|
*
|
* @Description: 字符串工具类<br>
|
* @Project: <br>
|
* @CreateDate: Created in 2020/2/4 13:58 <br>
|
* @Author: <a href="4345453@kidgrow.com">liuke</a>
|
*/
|
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|
private static final char SEPARATOR = '_';
|
private static final String CHARSET = "UTF-8";
|
|
/**
|
* 转换为字节数组
|
*
|
* @param str
|
* @return
|
*/
|
public static byte[] getBytes(String str) {
|
if (str != null) {
|
try {
|
return str.getBytes(CHARSET);
|
} catch (UnsupportedEncodingException e) {
|
return null;
|
}
|
} else {
|
return null;
|
}
|
}
|
|
/**
|
* 转换为字节数组
|
*
|
* @return
|
*/
|
public static String toString(byte[] bytes) {
|
try {
|
return new String(bytes, CHARSET);
|
} catch (UnsupportedEncodingException e) {
|
return EMPTY;
|
}
|
}
|
|
/**
|
* 转换为Boolean类型
|
* 'true', 'on', 'y', 't', 'yes' or '1' (case insensitive) will return true. Otherwise, false is returned.
|
*/
|
public static Boolean toBoolean(final Object val) {
|
if (val == null) {
|
return false;
|
}
|
return BooleanUtils.toBoolean(val.toString()) || "1".equals(val.toString());
|
}
|
|
|
/**
|
* 如果对象为空,则使用defaultVal值
|
* see: ObjectUtils.toString(obj, defaultVal)
|
*
|
* @param obj
|
* @param defaultVal
|
* @return
|
*/
|
public static String toString(final Object obj, final String defaultVal) {
|
return obj == null ? defaultVal : obj.toString();
|
}
|
|
/**
|
* 是否包含字符串
|
*
|
* @param str 验证字符串
|
* @param strs 字符串组
|
* @return 包含返回true
|
*/
|
public static boolean inString(String str, String... strs) {
|
if (str != null) {
|
for (String s : strs) {
|
if (str.equals(trim(s))) {
|
return true;
|
}
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 转换为Double类型
|
*/
|
public static Double toDouble(Object val) {
|
if (val == null) {
|
return 0D;
|
}
|
try {
|
return Double.valueOf(trim(val.toString()));
|
} catch (Exception e) {
|
return 0D;
|
}
|
}
|
|
/**
|
* 转换为Float类型
|
*/
|
public static Float toFloat(Object val) {
|
return toDouble(val).floatValue();
|
}
|
|
/**
|
* 转换为Long类型
|
*/
|
public static Long toLong(Object val) {
|
return toDouble(val).longValue();
|
}
|
|
/**
|
* 转换为Integer类型
|
*/
|
public static Integer toInteger(Object val) {
|
return toLong(val).intValue();
|
}
|
|
/**
|
* 缩略字符串(不区分中英文字符)
|
*
|
* @param str 目标字符串
|
* @param length 截取长度
|
* @return
|
*/
|
public static String ellipsis(String str, int length) {
|
if (str == null) {
|
return "";
|
}
|
try {
|
StringBuilder sb = new StringBuilder();
|
int currentLength = 0;
|
for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
|
currentLength += String.valueOf(c).getBytes("GBK").length;
|
if (currentLength <= length - 3) {
|
sb.append(c);
|
} else {
|
sb.append("...");
|
break;
|
}
|
}
|
return sb.toString();
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
/**
|
* 替换掉HTML标签方法
|
*/
|
public static String replaceHtml(String html) {
|
if (isBlank(html)) {
|
return "";
|
}
|
String regEx = "<.+?>";
|
Pattern p = Pattern.compile(regEx);
|
Matcher m = p.matcher(html);
|
String s = m.replaceAll("");
|
return s;
|
}
|
|
/**
|
* Html 转码.
|
*/
|
public static String escapeHtml(String html) {
|
return StringEscapeUtils.escapeHtml4(html);
|
}
|
|
/**
|
* Html 解码.
|
*/
|
public static String unescapeHtml(String htmlEscaped) {
|
return StringEscapeUtils.unescapeHtml4(htmlEscaped);
|
}
|
|
/**
|
* Xml 转码.
|
*/
|
public static String escapeXml(String xml) {
|
return StringEscapeUtils.escapeXml11(xml);
|
}
|
|
/**
|
* Xml 解码.
|
*/
|
public static String unescapeXml(String xmlEscaped) {
|
return StringEscapeUtils.unescapeXml(xmlEscaped);
|
}
|
|
/**
|
* url追加参数
|
*
|
* @param url 传入的url ex:"http://exp.kunnr.com/so/index.html?kunnrId=16&userProfile=16#/app/home"
|
* @param name 参数名
|
* @param value 参数值
|
* @return
|
* @author: xg.chen
|
* @date:2016年9月2日
|
*/
|
public static String appendURIParam(String url, String name, String value) {
|
url += (url.indexOf('?') == -1 ? '?' : '&');
|
url += EncodeUtils.encodeUrl(name) + '=' + EncodeUtils.encodeUrl(value);
|
return url;
|
}
|
|
/**
|
* 组装新的URL
|
*
|
* @param url
|
* @param map
|
* @return
|
*/
|
public static String appendURIParam(String url, Map<String, String> map) {
|
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
|
while (it.hasNext()) {
|
Map.Entry<String, String> entry = it.next();
|
url = appendURIParam(url, entry.getKey(), entry.getValue());
|
}
|
return url;
|
}
|
|
|
/**
|
* 驼峰转下划线
|
* createTime > create_time
|
*
|
* @param param
|
* @return
|
*/
|
public static String camelToUnderline(String param) {
|
if (param == null || "".equals(param.trim())) {
|
return "";
|
}
|
int len = param.length();
|
StringBuilder sb = new StringBuilder(len);
|
for (int i = 0; i < len; i++) {
|
char c = param.charAt(i);
|
if (Character.isUpperCase(c)) {
|
sb.append(SEPARATOR);
|
sb.append(Character.toLowerCase(c));
|
} else {
|
sb.append(c);
|
}
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 下划线转驼峰
|
* create_time > createTime
|
*
|
* @param param
|
* @return
|
*/
|
public static String underlineToCamel(String param) {
|
if (param == null || "".equals(param.trim())) {
|
return "";
|
}
|
StringBuilder sb = new StringBuilder(param);
|
Matcher mc = Pattern.compile(String.valueOf(SEPARATOR)).matcher(param);
|
int i = 0;
|
while (mc.find()) {
|
int position = mc.end() - (i++);
|
sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 格式化存储单位
|
*
|
* @param size byte 字节
|
* @return
|
*/
|
public static String formatBytes(long size) {
|
// 如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
|
int bytes = 1024;
|
if (size < bytes) {
|
return String.valueOf(size) + "Byte";
|
} else {
|
size = size / bytes;
|
}
|
// 如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位 //因为还没有到达要使用另一个单位的时候 //接下去以此类推
|
if (size < bytes) {
|
return String.valueOf(size) + "K";
|
} else {
|
size = size / bytes;
|
}
|
if (size < bytes) {
|
// 因为如果以MB为单位的话,要保留最后1位小数, //因此,把此数乘以100之后再取余
|
size = size * 100;
|
return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "M";
|
} else { // 否则如果要以GB为单位的,先除于1024再作同样的处理
|
size = size * 100 / bytes;
|
return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "G";
|
}
|
}
|
|
/**
|
* 匿名手机号
|
*
|
* @param mobile
|
* @return 152****4799
|
*/
|
public static String formatMobile(String mobile) {
|
|
if (isEmpty(mobile)) {
|
return null;
|
}
|
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
|
}
|
|
/**
|
* 匿名银行卡号
|
*
|
* @param bankCard
|
* @return
|
*/
|
public static String formatBankCard(String bankCard) {
|
if (isEmpty(bankCard)) {
|
return null;
|
}
|
return bankCard.replaceAll("(\\d{5})\\d{5}\\d{2}(\\d{4})", "$1****$2");
|
}
|
|
/**
|
* 匿名身份证
|
*
|
* @param idCard
|
* @return 4304*****7733
|
*/
|
public static String formatIdCard(String idCard) {
|
|
if (isEmpty(idCard)) {
|
return null;
|
}
|
return idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2");
|
}
|
|
/**
|
* 检测是否未手机号
|
*
|
* @param mobile
|
* @return
|
*/
|
public static boolean matchMobile(String mobile) {
|
if (mobile == null) {
|
return false;
|
}
|
String regex = "^((13[0-9][0-9])|14[5,7][0-9]|(15[^4,\\D][0-9])|(17[0-9][0-9])|(18[0-9][0-9]))\\d{7}$";
|
return Pattern.matches(regex,mobile);
|
}
|
|
/**
|
* 检测Email
|
*
|
* @param email
|
* @return
|
*/
|
public static boolean matchEmail(String email) {
|
if (email == null) {
|
return false;
|
}
|
String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
|
return Pattern.matches(regex, email);
|
}
|
|
|
/**
|
* 检测域名
|
* @param domain
|
* @return
|
*/
|
public static boolean matchDomain(String domain) {
|
if (domain == null) {
|
return false;
|
}
|
String regex = "^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";
|
return Pattern.matches(regex, domain);
|
}
|
|
/**
|
* 检测IP
|
* @param ip
|
* @return
|
*/
|
public static boolean matchIp(String ip) {
|
if (ip == null) {
|
return false;
|
}
|
String regex = "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$";
|
return Pattern.matches(regex, ip);
|
}
|
|
/**
|
* 检测HttpUrl
|
* @param url
|
* @return
|
*/
|
public static boolean matchHttpUrl(String url) {
|
if (url == null) {
|
return false;
|
}
|
String regex = "^(?=^.{3,255}$)(http(s)?:\\/\\/)?(www\\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\\d+)*(\\/\\w+\\.\\w+)*([\\?&]\\w+=\\w*)*$";
|
return Pattern.matches(regex, url);
|
}
|
|
/**
|
* 校验银行卡卡号
|
* 校验过程:
|
* 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。
|
* 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和。
|
* 3、将奇数位总和加上偶数位总和,结果应该可以被10整除。
|
*/
|
public static boolean matchBankCard(String bankCard) {
|
if (bankCard == null) {
|
return false;
|
}
|
if (bankCard.length() < 15 || bankCard.length() > 19) {
|
return false;
|
}
|
char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
|
if (bit == 'N') {
|
return false;
|
}
|
return bankCard.charAt(bankCard.length() - 1) == bit;
|
}
|
|
/**
|
* 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
|
*
|
* @param nonCheckCodeBankCard
|
* @return
|
*/
|
public static char getBankCardCheckCode(String nonCheckCodeBankCard) {
|
if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
|
|| !nonCheckCodeBankCard.matches("\\d+")) {
|
//如果传的不是数据返回N
|
return 'N';
|
}
|
char[] chs = nonCheckCodeBankCard.trim().toCharArray();
|
int luhmSum = 0;
|
for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
|
int k = chs[i] - '0';
|
if (j % 2 == 0) {
|
k *= 2;
|
k = k / 10 + k % 10;
|
}
|
luhmSum += k;
|
}
|
return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
|
}
|
|
|
/**
|
* 处理非法字符
|
*/
|
private static List<Pattern> patterns = null;
|
|
private static List<Object[]> getXssPatternList() {
|
List<Object[]> ret = new ArrayList<Object[]>();
|
ret.add(new Object[]{"<(no)?script[^>]*>.*?</(no)?script>", Pattern.CASE_INSENSITIVE});
|
ret.add(new Object[]{"eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
|
ret.add(new Object[]{"expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
|
ret.add(new Object[]{"(javascript:|vbscript:|view-source:)*", Pattern.CASE_INSENSITIVE});
|
ret.add(new Object[]{"<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
|
ret.add(new Object[]{"(window\\.location|window\\.|\\.location|document\\.cookie|document\\.|alert\\(.*?\\)|window\\.open\\()*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
|
ret.add(new Object[]{"<+\\s*\\w*\\s*(oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|ondragover|ondragstart|ondrop|onerror=|onerroupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmousout|onmouseover|onmouseup|onmousewheel|onmove|onmoveend|onmovestart|onabort|onactivate|onafterprint|onafterupdate|onbefore|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|onpaste|onpropertychange|onreadystatuschange|onreset|onresize|onresizend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload)+\\s*=+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
|
return ret;
|
}
|
|
private static List<Pattern> getPatterns() {
|
|
if (patterns == null) {
|
|
List<Pattern> list = new ArrayList<Pattern>();
|
|
String regex = null;
|
Integer flag = null;
|
int arrLength = 0;
|
|
for (Object[] arr : getXssPatternList()) {
|
arrLength = arr.length;
|
for (int i = 0; i < arrLength; i++) {
|
regex = (String) arr[0];
|
flag = (Integer) arr[1];
|
list.add(Pattern.compile(regex, flag));
|
}
|
}
|
|
patterns = list;
|
}
|
|
return patterns;
|
}
|
|
public static String stripXss(String value) {
|
if (StringUtils.isNotBlank(value)) {
|
Matcher matcher = null;
|
for (Pattern pattern : getPatterns()) {
|
matcher = pattern.matcher(value);
|
if (matcher.find()) {
|
value = matcher.replaceAll("");
|
}
|
}
|
value = value.replaceAll("<", "<").replaceAll(">", ">");
|
}
|
return value;
|
}
|
|
/**
|
* 密码强度
|
*
|
* @return Z = 字母 S = 数字 T = 特殊字符
|
*/
|
public static String checkPassword(String passwordStr) {
|
String regexZ = "\\d*";
|
String regexS = "[a-zA-Z]+";
|
String regexT = "\\W+$";
|
String regexZT = "\\D*";
|
String regexST = "[\\d\\W]*";
|
String regexZS = "\\w*";
|
String regexZST = "[\\w\\W]*";
|
|
if (passwordStr.matches(regexZ)) {
|
return "弱";
|
}
|
if (passwordStr.matches(regexS)) {
|
return "弱";
|
}
|
if (passwordStr.matches(regexT)) {
|
return "弱";
|
}
|
if (passwordStr.matches(regexZT)) {
|
return "中";
|
}
|
if (passwordStr.matches(regexST)) {
|
return "中";
|
}
|
if (passwordStr.matches(regexZS)) {
|
return "中";
|
}
|
if (passwordStr.matches(regexZST)) {
|
return "强";
|
}
|
return passwordStr;
|
}
|
|
|
/**
|
* 将 Exception 转化为 String
|
*/
|
public static String getExceptionToString(Throwable e) {
|
if (e == null){
|
return "";
|
}
|
StringWriter stringWriter = new StringWriter();
|
e.printStackTrace(new PrintWriter(stringWriter));
|
return stringWriter.toString();
|
}
|
|
public static void main(String[] args) {
|
System.out.println(StringUtils.matchDomain("515608851@qq.com"));
|
System.out.println(StringUtils.matchDomain("www.qq.com"));
|
System.out.println(StringUtils.matchDomain("qq.com"));
|
System.out.println(StringUtils.matchIp("qq.com"));
|
System.out.println(StringUtils.matchIp("192.168.0.1"));
|
// System.out.println("test");
|
// System.out.println(checkPassword("f0a2adfdf56241bf839d714f7f74f4d1"));
|
/* String value = null;
|
value = stripXss("<script language=text/javascript>alert(document.cookie);</script>");
|
System.out.println("type-1: '" + value + "'");
|
|
value = stripXss("<script src='' onerror='alert(document.cookie)'></script>");
|
System.out.println("type-2: '" + value + "'");
|
|
value = stripXss("</script>");
|
System.out.println("type-3: '" + value + "'");
|
|
value = stripXss(" eval(abc);");
|
System.out.println("type-4: '" + value + "'");
|
|
value = stripXss(" expression(abc);");
|
System.out.println("type-5: '" + value + "'");
|
|
value = stripXss("<img src='' onerror='alert(document.cookie);'></img>");
|
System.out.println("type-6: '" + value + "'");
|
|
value = stripXss("<img src='' onerror='alert(document.cookie);'/>");
|
System.out.println("type-7: '" + value + "'");
|
|
value = stripXss("<img src='' onerror='alert(document.cookie);'>");
|
System.out.println("type-8: '" + value + "'");
|
|
value = stripXss("<script language=text/javascript>alert(document.cookie);");
|
System.out.println("type-9: '" + value + "'");
|
|
value = stripXss("<script>window.location='url'");
|
System.out.println("type-10: '" + value + "'");
|
|
value = stripXss(" onload='alert(\"abc\");");
|
System.out.println("type-11: '" + value + "'");
|
|
value = stripXss("<img src=x<!--'<\"-->>");
|
System.out.println("type-12: '" + value + "'");
|
|
value = stripXss("<=img onstop=");
|
System.out.println("type-13: '" + value + "'");*/
|
|
// System.out.println(formatBytes(1222));
|
//System.out.println(underlineToCamel("create_time"));
|
}
|
|
}
|