/**
* excludeAType=true ���Թ���A���127��ͷ��IP��ַ�����ⵥ��A06D18683
* @param strIp
* @param excludeAType
* @return
*/
public static long ipToLong(String strIp, boolean excludeAType)
{
if (!ipValid(strIp, excludeAType))
{
return 0;
}
int result = 0;
try
{
InetAddress addr = InetAddress.getByName(strIp);
byte[] bas = addr.getAddress();
result |= ((short) (bas[0] & 0x00ff) << 24);
result |= ((short) (bas[1] & 0x00ff) << 16);
result |= ((short) (bas[2] & 0x00ff) <<
;
result |= (short) (bas[3] & 0x00ff);
long ret = result >>> 1;
ret = ret << 1;
ret |= (result << 31) >>> 31;
return ret;
}
catch (UnknownHostException e)
{
logger.error(e.getMessage(), e);
}
return 0L;
}
public static String longToIP(Long longIp)
{
// StringBuffer sbIp = new StringBuffer();
// sbIp.append(longIp >>> 24);
// sbIp.append(".");
// sbIp.append((longIp & 0x00FFFFFF) >>> 16);
// sbIp.append(".");
// sbIp.append((longIp & 0x0000FFFF) >>>
;
// sbIp.append(".");
// sbIp.append(longIp & 0x000000FF);
// return sbIp.toString();
if (longIp == null || longIp <= 0)
{
return "";
}
ByteBuffer buf = ByteBuffer.allocate(4);
int ret = (int) (longIp & 0x00000000ffffffff);
buf.putInt(ret);
InetAddress addr = null;
String ipAddress = "";
try
{
addr = InetAddress.getByAddress(buf.array());
ipAddress = addr.getHostAddress();
}
catch (UnknownHostException e)
{
logger.error(e.getMessage(), e);
}
return ipAddress;
}
/**
* ������ķ�����ͬ��ֻ�Dz���ΪLong
* @param ip
* @return
*/
public static boolean ipValid(Long ip)
{
String ips = longToIpStd(ip);
return ipValid(ips);
}