stringUtils字符串⼯具类
llect.Lists;
import org.apachemons.lang3.StringEscapeUtils;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.URLDecoder;
import java.URLEncoder;
CharacterIterator;
StringCharacterIterator;
import java.util.List;
import java.util.Locale;
import Matcher;
import Pattern;
/**
* 字符串⼯具类, 继承org.apachemons.lang3.StringUtils类
*/
public class StringUtils extends org.apachemons.lang3.StringUtils {
private static final char SEPARATOR = '_';
private static final String CHARSET_NAME = "UTF-8";
/**
* 转换为字节数组
*
* @param str
* @return
*/
public static byte[] getBytes(String str) {
if (str != null) {
try {
Bytes(CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
return null;
}
} else {
return null;
}
}
/**
* 字节数组转换为String
*
* @param bytes
* @return
*/
public static String toString(byte[] bytes) {
try {
return new String(bytes, CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
return EMPTY;
}
}
/**
* 是否包含字符串
*
* @param str 验证字符串
* @param strs 字符串组
* @return 包含返回true
*/
public static boolean inString(String str, strs) {
if (str != null) {
for (String s : strs) {
if (str.equals(trim(s))) {
return true;
}
}
}
return false;
}
/**
* 替换掉HTML标签⽅法
*/
public static String unescapeHtml(String html) {
if (isBlank(html)) {
return "";
}
return StringEscapeUtils.unescapeHtml4(html);
}
/**
* 特殊符号转义防跨站脚本攻击
*
* @param aText
* @return
*/
replaceall()
public static String HTMLEncode(String aText) {
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(aText); char character = iterator.current();
while (character != CharacterIterator.DONE) {
if (character == '<') {
result.append("<");
} else if (character == '>') {
result.append(">");
} else if (character == '&') {
result.append("&");
} else if (character == '\"') {
result.append(""");
} else {
//the char is not a special one
//add it to the result as is
result.append(character);
}
character = ();
}
String();
}
/**
* 替换掉HTML标签⽅法
*/
public static String replaceHtml(String html) {
if (isBlank(html)) {
return "";
}
String regEx = "<.+?>";
String regEx = "<.+?>";
Pattern p = Patternpile(regEx);
Matcher m = p.matcher(html);
String s = m.replaceAll("");
return s;
}
/**
* 替换为⼿机识别的HTML,去掉样式及属性,保留回车。
*
* @param html
* @return
*/
public static String replaceMobileHtml(String html) {
if (html == null) {
return "";
}
placeAll("<([a-z]+?)\\s+?.*?>", "<$1>");
}
/**
* 替换为⼿机识别的HTML,去掉样式及属性,保留回车。
*
* @param txt
* @return
*/
public static String toHtml(String txt) {
if (txt == null) {
return "";
}
return replace(replace(EncodesUitls.escapeHtml(txt), "\n", "
"), "\t", " ");
}
/**
* 缩略字符串(不区分中英⽂字符)
*
* @param str ⽬标字符串
* @param length 截取长度
* @return
*/
public static String abbr(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;
}
}
String();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
public static String abbr2(String param, int length) {
if (param == null) {
if (param == null) {
return "";
}
StringBuffer result = new StringBuffer();
int n = 0;
char temp;
boolean isCode = false; // 是不是HTML代码
boolean isHTML = false; // 是不是HTML特殊字符,如
for (int i = 0; i < param.length(); i++) {
temp = param.charAt(i);
if (temp == '<') {
isCode = true;
} else if (temp == '&') {
isHTML = true;
} else if (temp == '>' && isCode) {
n = n - 1;
isCode = false;
} else if (temp == ';' && isHTML) {
isHTML = false;
}
try {
if (!isCode && !isHTML) {
n += String.valueOf(temp).getBytes("GBK").length;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (n <= length - 3) {
result.append(temp);
} else {
result.append("...");
break;
}
}
// 取出截取字符串中的HTML标记
String temp_result = String().replaceAll("(>)[^<>]*(
]*/?>",
"");
// 去掉成对的HTML标记
temp_result = placeAll("<([a-zA-Z]+)[^<>]*>(.*?) ",
"$2");
// ⽤正则表达式取出标记
Pattern p = Patternpile("<([a-zA-Z]+)[^<>]*>");
Matcher m = p.matcher(temp_result);
List
endHTML = wArrayList();
while (m.find()) {
endHTML.up(1));
}
// 补全不成对的HTML标记
for (int i = endHTML.size() - 1; i >= 0; i--) {
result.append("
");
}
String();
}
/**
* 转换为Double类型
*/
public static Double toDouble(Object val) {
if (val == null) {
return 0D;
}
try {
return Double.valueOf(String()));
} 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();
}
/**
* 获得⽤户远程地址
*/
public static String getRemoteAddr(HttpServletRequest request) {
String remoteAddr = Header("X-Real-IP");
if (isNotBlank(remoteAddr)) {
remoteAddr = Header("X-Forwarded-For");
} else if (isNotBlank(remoteAddr)) {
remoteAddr = Header("Proxy-Client-IP");
} else if (isNotBlank(remoteAddr)) {
remoteAddr = Header("WL-Proxy-Client-IP");
}
return remoteAddr != null ? remoteAddr : RemoteAddr(); }
/**
* 获得⽤户远程IP地址 Mac地址
*/
public static String getMACAddress(String ip) {
String result = "";
String[] cmd = {
"cmd",
"/c",
"ping " + ip
};
String[] another = {
"cmd",
"/c",
"arp -a"
};
String cmdResult = callCmd(cmd, another);
result = filterMacAddress(ip, cmdResult, "-");
return result;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论