/**
* 对象转字节(序列化)
*
* @param obj
* @return
*/
public static byte[] objectToByte(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bo = null;
ObjectOutputStream oo = null;
try {
bo = new ByteArrayOutputStream();
oo = new ObjectOutputStream(bo);
oo.writeObject(obj);
bytes = bo.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bo != null)
bo.close();
if (oo != null)
oo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
/**
* 字节转对象(反序列化)
*
* @param bytes
* @return
*/
public static Object byteToObject(byte[] bytes) {
Object obj = null;
ByteArrayInputStream bi = null;
ObjectInputStream oi = null;
try {
bi = new ByteArrayInputStream(bytes);
oi = new ObjectInputStream(bi);
obj = oi.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bi != null)
bi.close();
if (oi != null)
oi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 判断是否为空
* @param objects
* @return
*/
public static boolean isNUll(Object... objects) {
for (Object object : objects) {
if (object == null || "".equals(object) ) {
return true;
}
}
return false;
}
/**
* 从request中取值
* @param request
* @return
*/
public static Map<String, Object> getParam(HttpServletRequest request){
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> map = new HashMap<String, Object>(parameterMap.size());
parameterMap.forEach((k,v) -> {
if (!isNUll(v[0])) {
map.put(k, v[0]);
}
});
return map;
}
/**
* 将字符串转为Integer
* @param string
* @return
*/
public static Integer str2int(String string) {
if (isNUll(string)) {
return null;
}
Integer val = 0;
try {
val = Integer.valueOf(string);
} catch (NumberFormatException e) {
// TODO: handle exception
}
return val;
}
/**
* 将数据转为字符串
* @param object
* @return
*/
public static String array2str(Object[] object) {
if (isNUll(object) || object.length < 1) {
return null;
}
StringBuffer stringBuffer = new StringBuffer();
for (Object obj : object) {
stringBuffer.append(obj.toString().trim()).append(Constant.GOODS_SEPARAMETOR);
}
return stringBuffer.toString().substring(0,stringBuffer.length()-1);
}
/**
* MD5加密
*
* @param str
* @return
*/
public static String encrypt(String str) {
return encrypt(str, "md5");
}
/**
* 信息摘要加密
*
* @param str
* @return
*/
public static String encrypt(String str, String type) {
if (isNUll(str)) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance(type);
byte[] bys = md.digest(str.getBytes());
return new BASE64Encoder().encode(bys);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取请求的IP地址
* 在apache+WebLogic整合系统中直接用request.getRemoteAddr()是无法取到真正的客户IP的。<br>
* apache会增加下列头信息:<br>
* X-Forwarded-For=21.1178.21.1 <br>
* WL-Proxy-Client-IP=21.1178.21.1
*
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request) {
// 只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项
String ip = request.getHeader("X-Forwarded-For");
// 如果客户端经过多级反向代理,则X-Forwarded-For的值并不止一个,而是一串IP值,
// 取X-Forwarded-For中第一个非unknown的有效IP字符串即为用户真实IP
String ipResult = getIp(ip);
if (ipResult != null) {
return ipResult;
}
// 代理服务器ip
ip = request.getHeader("Proxy-Client-IP");
ipResult = getIp(ip);
if (!isNUll(ipResult) && !"unknown".equals(ipResult)) {
return ipResult;
}
// weblogic集群下,获取客户端ip
ip = request.getHeader("WL-Proxy-Client-IP");
ipResult = getIp(ip);
if (!isNUll(ipResult) && !"unknown".equals(ipResult)) {
return ipResult;
}
ip = request.getRemoteAddr();
ipResult = getIp(ip);
if (!isNUll(ipResult) && !"unknown".equals(ipResult)) {
return ipResult;
}
return "";
}
/**
* 获取ip
*
* @param ips
* @return
*/
private static String getIp(String ips) {
if (isNUll(ips)) {
return null;
}
String[] tokens = ips.split(",");
for (String s : tokens) {
if (isIp(s.trim())) {
return s.trim();
}
}
return null;
}
/**
* 给定的字符串是不是ip地址
*
* @param value
* @return
*/
public static boolean isIp(String value) {
final String regValidatorIp = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$";
if (isNUll(value))
return false;
return value.matches(regValidatorIp);
}