public class StringUtil { private static final Pattern IPV4_PATTERN = Pattern.compile( "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"); private static final Pattern IPV6_STD_PATTERN = Pattern.compile( "^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile( "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$"); public static boolean isIPv4Address(final String input) { return IPV4_PATTERN.matcher(input).matches(); } public static boolean isIPv6StdAddress(final String input) { return IPV6_STD_PATTERN.matcher(input).matches(); } public static boolean isIPv6HexCompressedAddress(final String input) { return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches(); } public static boolean isIPv6Address(final String input) { return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input); } /** * 计算MD5密码 * * @param loginname * @param password * @param createAt * @return */ public static String getPassword(String loginname, String password, int createAt) { String p = Lang.md5(Lang.md5(password) + loginname + createAt); return 'w' + p.substring(0, p.length() - 1); } /** * 获得用户远程地址 */ public static String getRemoteAddr() { HttpServletRequest request = Mvcs.getReq(); String remoteAddr = request.getHeader("X-Real-IP"); if (Strings.isNotBlank(remoteAddr)) { remoteAddr = request.getHeader("X-Forwarded-For"); } else if (Strings.isNotBlank(remoteAddr)) { remoteAddr = request.getHeader("Proxy-Client-IP"); } else if (Strings.isNotBlank(remoteAddr)) { remoteAddr = request.getHeader("WL-Proxy-Client-IP"); } String ip = remoteAddr != null ? remoteAddr : Strings.sNull(request.getRemoteAddr()); if (isIPv4Address(ip) || isIPv6Address(ip)) { return ip; } return ""; } /** * 去掉URL中?后的路径 * * @param p * @return */ public static String getPath(String p) { if (Strings.sNull(p).contains("?")) { return p.substring(0, p.indexOf("?")); } return Strings.sNull(p); } /** * 获得父节点ID * * @param s * @return */ public static String getParentId(String s) { if (!Strings.isEmpty(s) && s.length() > 4) { return s.substring(0, s.length() - 4); } return ""; } /** * 得到n位随机数 * * @param s * @return */ public static String getRndNumber(int s) { Random ra = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < s; i++) { sb.append(String.valueOf(ra.nextInt(8))); } return sb.toString(); } /** * 判断是否以字符串开头 * * @param str * @param s * @return */ public boolean startWith(String str, String s) { return Strings.sNull(str).startsWith(Strings.sNull(s)); } /** * 判断是否包含字符串 * * @param str * @param s * @return */ public boolean contains(String str, String s) { return Strings.sNull(str).contains(Strings.sNull(s)); } }