/**
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获得所在地
* 如: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;
数据库如图: