iputil1

package com.security.dpi.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* IP��ַ��ع�����
*
* <p>
* detailed comment
* @author james 2008-9-10
* @see
* @since 1.0
*/
public class IpUtils
{
static long IP_SPAN_MAX_SIZE = 100000;

private static Log logger = LogFactory.getLog(IpUtils.class);

public static long ipToLong(String strIp)
{
if (!ipValid(strIp))
{
return 0;
}
// //String ip[] = strIp.split("\\.");
// /* ��λip��ַ�ַ��еġ�.����λ�� */
// int position1 = strIp.indexOf(".");
// int position2 = strIp.indexOf(".", position1 + 1);
// int position3 = strIp.indexOf(".", position2 + 1);
// /* ��ÿ��.֮����ַ�ת����long */
// long one = Long.parseLong(strIp.substring(0, position1));
// long two = Long.parseLong(strIp.substring(position1 + 1, position2));
// long three = Long.parseLong(strIp.substring(position2 + 1,
// position3));
// long four = Long.parseLong(strIp.substring(position3 + 1));

// return (one << 24) + (two << 16) + (three << 8) + four;
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) << 8);
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值