常用的工具类
public class StringUtil {
public static String toString(Object object){
return object == null ? "" : object.toString();
}
public static String toString(Object object,String defaultValue){
return object == null ? defaultValue : object.toString();
}
public static int toInt(Object obj,int defaultValue) {
try {
return Integer.valueOf(obj.toString());
} catch (Exception e){
return defaultValue;
}
}
public static double toDouble(Object obj,double defaultValue){
try {
return Double.valueOf(obj.toString());
} catch (Exception e) {
return defaultValue;
}
}
public static boolean isNotEmpty(Object obj) {
return obj != null && !"".equals(obj.toString().trim());
}
public static String getUUID32() {
return UUID.randomUUID().toString().replace("-", "");
}
public static boolean availableStr(String str){
if(str != null && !"".equals(str) && !"".equals(str.trim())){
return true;
}else{
return false;
}
}
public static boolean verifyFileName(String param) {
if (param == null || "".equals(param)||param.length()>255) {
return false;
}
if (param.indexOf("..")>-1||param.indexOf("//")>-1||param.indexOf("")>-1){
return false;
}
String regEx = "[^*|\\:\"<>?]+\\.[^*|\\:\"<>?\u4E00-\u9FA5]+";
return param.matches(regEx);
}
public static boolean verifyFilePath(String param) {
if (param == null || "".equals(param)||param.length()>255) {
return false;
}
if (param.indexOf("..")>-1||param.indexOf("//")>-1||param.indexOf("")>-1){
return false;
}
String regEx = "[^*|\"<>?\\.\u4E00-\u9FA5]+";
return param.matches(regEx);
}
public static String filePathFilter(String path){
if (path == null) return null;
String cleanString = "";
for (int i = 0; i < path.length(); ++i) {
cleanString += cleanChar(path.charAt(i));
}
if (cleanString.indexOf("..")>0||cleanString.indexOf("//")>0||cleanString.indexOf("\\\\")>0||
cleanString.indexOf("https")>0||cleanString.indexOf("http")>0){
cleanString = "error.jpg";
}
if (cleanString.indexOf("\\")>-1){
cleanString = cleanString.replaceAll("\\\\",Matcher.quoteReplacement(File.separator));
}
if (cleanString.indexOf("/")>-1){
cleanString = cleanString.replaceAll("/",Matcher.quoteReplacement(File.separator));
}
return cleanString;
}
public static String filterInput(String input) {
List<String> list = new ArrayList<String>();
list.add("<");
list.add(">");
list.add("(");
list.add(")");
list.add("&");
list.add("?");
list.add(";");
String encode = Normalizer.normalize(input, Normalizer.Form.NFKC);
for (int i=0;i<list.size();i++) {
encode = encode.replace(list.get(i), "");
}
return encode;
}
public static String filterStr(String str){
String resValue = scriptPattern.matcher(str).replaceAll("");
resValue = singleScriptPattern.matcher(resValue).replaceAll("");
resValue = singleScriptPattern2.matcher(resValue).replaceAll("");
resValue = evalScriptPattern.matcher(resValue).replaceAll("");
resValue = onloadScriptPattern.matcher(resValue).replaceAll("");
resValue = onmousScriptPattern.matcher(resValue).replaceAll("");
resValue = alertScriptPattern.matcher(resValue).replaceAll("");
resValue = iframeScriptPattern.matcher(resValue).replaceAll("");
resValue = aTagPattern.matcher(resValue).replaceAll("");
resValue = otherScriptPattern.matcher(resValue).replaceAll("");
resValue = fontfamilyPattern.matcher(resValue).replaceAll("");
Pattern pa = Pattern.compile("[`~!@$%^&*()\\+\\=\\{}|:\"?><【】\\/\\r\\n]");
Matcher ma = pa.matcher(resValue);
if(ma.find()){
resValue = ma.replaceAll("");
}
return resValue;
}
public static Map<String,String> filterMapValue(Map<String,String> param){
Map<String,String> res = new HashMap<>();
for (String key:param.keySet()){
String value = StringUtil.toString(param.get(key));
value = filterStr(value);
res.put(key,value);
}
return res;
}
public static Map<String,Object> filterMapValueObject(Map<String,Object> param){
Map<String,Object> res = new HashMap<>();
for (String key:param.keySet()){
if (param.get(key) instanceof String){
String value = StringUtil.toString(param.get(key));
value = filterStr(value);
res.put(key,value);
}else {
res.put(key,param.get(key));
}
}
return res;
}
public static List<Map<String,String>> filterListMap(List<Map<String,String>> param){
if (param!=null&¶m.size()>=1){
List<Map<String,String>> res = new ArrayList<>();
for (int i = 0;i<param.size();i++){
Map<String,String> map = filterMapValue(param.get(i));
res.add(map);
}
return res;
}
return param;
}
public static void filterListObject(List param) throws Exception{
if (param != null&¶m.size()>=1){
for (int i = 0; i < param.size();i++){
Field[] fields = param.get(i).getClass().getDeclaredFields();
for (int j = 0;j<fields.length;j++){
String name = fields[j].getName().replaceFirst(fields[j].getName().substring(0, 1)
, fields[j].getName().substring(0, 1).toUpperCase());
String type = fields[j].getGenericType().toString();
if (type.equals("class java.lang.String")){
Method[] methods = param.get(i).getClass().getMethods();
if (hasMethod(methods,"get"+name)&&hasMethod(methods,"set"+name)){
Method m = param.get(i).getClass().getMethod("get" + name);
String value = (String) m.invoke(param.get(i));
if (value != null) {
m = param.get(i).getClass().getMethod("set" + name, String.class);
m.invoke(param.get(i), filterStr(value));
}
}
}
}
}
}
}
private static boolean hasMethod(Method[] methods,String name){
if (methods==null||methods.length==0){
return false;
}
for (int i =0; i < methods.length;i++){
if (methods[i].getName().equals(name)){
return true;
}
}
return false;
}
public static String[] filterStrArr(String[] param){
if (param == null){
return null;
}
if (param.length >0){
for (int i =0;i < param.length;i++){
param[i] = filterStr(param[i]);
}
}
return param;
}
public static List<String> filterStrList(List<String> param){
if (param==null||param.size()==0){
return param;
}
List<String> res = new ArrayList<>();
for (int i =0;i<(param.size()>MAX_RESULT_SIZE?MAX_RESULT_SIZE:param.size());i++){
res.add(filterStr(param.get(i)));
}
return res;
}
public static int getCharNumber(String str,String ch){
if (str == null || str.length()==0 || ch.indexOf(ch)<0){
return -1;
}
int count = 0;
int index = 0;
do{
index = str.indexOf(ch,index);
if (index>-1){
count++;
index = index+ch.length();
}
}while (index>-1);
return count;
}
}