根据IP地址十进制范围--获得IP地址所在地

来源:互联网 发布:js object 添加元素 编辑:程序博客网 时间:2024/04/30 17:25
/**

* 根据IP获得所在地

        *  如:42.80.195.109

* */
public String getLocationByIP(String strIP) throws Exception {
try{
if("0:0:0:0:0:0:0:1".equals(strIP)) {
return "本机";
}else if("127.0.0.1".equals(strIP)) {
return "本机保留地址";
}else {
String[] strs = strIP.split("\\.");
if(strs != null && strs.length > 0) {
long num = Long.valueOf(strs[0])*256*256*256 
+ Long.valueOf(strs[1])*256*256
+ Long.valueOf(strs[2])*256
+ Long.valueOf(strs[3]);

String location = userMapper.getLocationByIpNum(num);
return StringUtils.isNotEmpty(location) ? location :  "未知";
}
return "未知";
}
}catch(IOException e){
return null;
}

}


/**
* 根据IP获得地址
* */
@Select("select country1 from ipaddr where startnum <= #{IpNum} and endnum >= #{IpNum} limit 1")
public String getLocationByIpNum(long IpNum) throws Exception;


数据库如图:


0 0