常用的java程式設計工具類

2021-08-20 10:40:42 字數 2824 閱讀 6472

/**

* 物件轉位元組(序列化)

* * @param obj

* @return

*/ public static byte objecttobyte(object obj) catch (exception e) finally catch (ioexception e)

} return bytes;

}

/**

* 位元組轉物件(反序列化)

* * @param bytes

* @return

*/public static object bytetoobject(byte bytes) catch (exception e) finally catch (ioexception e)

}return obj;

}/**

* 判斷是否為空

* @param objects

* @return

*/public static boolean isnull(object... objects)

}return false;

}/**

* 從request中取值

* 將字串轉為integer

* @param string

* @return

*/public static integer str2int(string string)

integer val = 0;

try catch (numberformatexception e)

return val;

}/**

* 將資料轉為字串

* @param object

* @return

*/public static string array2str(object object)

stringbuffer stringbuffer = new stringbuffer();

for (object obj : object)

return stringbuffer.tostring().substring(0,stringbuffer.length()-1);

}/**

* md5加密

* * @param str

* @return

*/public static string encrypt(string str)

/** * 資訊摘要加密

* * @param str

* @return

*/public static string encrypt(string str, string type)

try catch (nosuchalgorithmexception e)

return null;

}/**

* 獲取請求的ip位址

* 在apache+weblogic整合系統中直接用request.getremoteaddr()是無法取到真正的客戶ip的。

* apache會增加下列頭資訊:

// **伺服器ip

ip = request.getheader("proxy-client-ip");

ipresult = getip(ip);

if (!isnull(ipresult) && !"unknown".equals(ipresult))

// weblogic集群下,獲取客戶端ip

ip = request.getheader("wl-proxy-client-ip");

ipresult = getip(ip);

if (!isnull(ipresult) && !"unknown".equals(ipresult))

ip = request.getremoteaddr();

ipresult = getip(ip);

if (!isnull(ipresult) && !"unknown".equals(ipresult))

return "";

}/**

* 獲取ip

* * @param ips

* @return

*/private static string getip(string ips)

string tokens = ips.split(",");

for (string s : tokens)

}return null;

}/**

* 給定的字串是不是ip位址

* * @param value

* @return

*/public static boolean isip(string value) \\.[0-9]\\.[0-9]\\.[0-9]$";

if (isnull(value))

return false;

return value.matches(regvalidatorip);

}

常用JAVA工具類

獲取訪問者ip 獲取訪問者ip 獲取使用者瀏覽器型別 獲取使用者瀏覽器型別 獲取時間差 獲取相差n月的月份的第一天或最後一天,格式 yyyymmdd param diff 月份差值 param type first 該月第一天,last 該月最後一天 return yyyymmdd public s...

JAVA常用的util工具類

前言 日常開發中,我們需要封裝些基礎的方法來簡化我們的開發 本文是對一些常用的基礎方法進行總結,方便日後使用。更新.wanghaifeng 判斷是否空 param obj return public static boolean checknull object obj else if obj in...

JAVA 併發程式設計 同步工具類

countdownlatch 閉鎖 閉鎖是一種同步工具類,可以延遲線程的進度知道其到達終止狀態。閉鎖的作用相當於一扇門 await 在閉鎖到達結束狀態之前,這扇門一直是關閉的,不允許任何執行緒通過,當到達結束狀態時 所有執行緒均到達countdown 這扇門會開啟並且允許所有的執行緒通過。而且,當門...