前言
提示:工作中总结了根据IP地址获取地理位置的工具类,有需要的小伙伴可供参考(注:我使用的IP库为IPPlus360库)
代码如下:
import com.bytefen.entity.IPAddressEntity;
import com.bytefen.util.mapper.JsonMapper;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.ipplus360.db.AWReader;
import java.io.File;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;
/**
* @ClassName: IPPlus360Util
* @Author: Binean
*/
public class IPPlus360Util {
public static IPAddressEntity finIPGeo(String ipAddress) {
IPAddressEntity info = new IPAddressEntity();
try {ip
String ip = ipAddress;
String ipipDBFile = PropertyUtil.getProperty("ipplus360库文件路径");
String filePath = ipipDBFile;
AWReader awReader = new AWReader(new File(filePath));
InetAddress address = InetAddress.getByName(ip);
JsonNode jsonNode = awReader.get(address);
JsonMapper mapper = JsonMapper.nonEmptyMapper();
String string = mapper.toJson(jsonNode);
JavaType javaType = mapper.contructMapType(Map.class, String.class, Object.class);
Map<String, Object> map = mapper.fromJson(string, javaType);
String country = StringUtil.trim(map.get("country"));
String isp = StringUtil.trim(map.get("owner"));
String newIsp = null;
byte[] bytes = isp.getBytes();
int i = bytes.length;//i为字节长度
int j = isp.length();//j为字符长度
if (i != j) {
newIsp = isp.substring(isp.length() - 2);
} else {
newIsp = isp;
}
List<Map<String, Object>> multiAreas = (List<Map<String, Object>>) map.get("multiAreas");
String newProv = null;
String newCity = null;
for (Map<String, Object> multiArea : multiAreas) {
String prov = StringUtil.trim(multiArea.get("prov"));
if (!StringUtil.isEmpty(prov)) {
newProv = prov.substring(0, prov.length() - 1);
}
String city = StringUtil.trim(multiArea.get("city"));
if (!StringUtil.isEmpty(prov)) {
newCity = city.substring(0, city.length() - 1);
}
}
info.setCountry(country);
info.setProv(newProv);
info.setCity(newCity);
info.setIsp(newIsp);
return info;
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
public static String findGEOAddress(String ipAddress) {
if (IPPlus360Util.isIP(ipAddress)) {
IPAddressEntity ipAddressEntity = IPPlus360Util.finIPGeo(ipAddress);
if (ipAddressEntity == null) {
return null;
}
String country = ipAddressEntity.getCountry();
String regionCity = (ipAddressEntity.getProv() == null) ? "" : ipAddressEntity.getProv();
if (!StringUtil.isEmpty(ipAddressEntity.getCity()) && !regionCity.equals(ipAddressEntity.getCity())) {
regionCity += " " + ipAddressEntity.getCity();
}
String area = country;
if (!StringUtil.isEmpty(regionCity) && !area.equals(regionCity)) {
area += " " + regionCity;
}
String isp = StringUtil.isEmpty(ipAddressEntity.getIsp()) ? null : ipAddressEntity.getIsp();
if (isp != null) {
area += " " + isp;
}
return area;
} else {
return null;
}
}
//判断输入字符串是否为IP的方法
public static boolean isIP(String str){
if(StringUtil.isEmpty(str)){
return false;
}
String p = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[0-9])\\."
+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
Pattern pattern = Pattern.compile(p);
return pattern.matcher(str).matches();
}
}
下面附上返回参数接收实体类(IPAddressEntity.Java):
/**
*@author Binean
*IPPlus360返回参数接收实体类
*/
public class IPAddressEntity {
private String country;
private String prov;
private String city;
private String isp;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProv() {
return prov;
}
public void setProv(String prov) {
this.prov = prov;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getIsp() {
return isp;
}
public void setIsp(String isp) {
this.isp = isp;
}
@Override
public String toString() {
return "IPAddressEntity{" +
"country='" + country + '\'' +
", prov='" + prov + '\'' +
", city='" + city + '\'' +
", isp='" + isp + '\'' +
'}';
}
}
提示:这里pom文件需要导入com.ipplus360.db的jar包
jar包下载地址:
https://gitee.com/shao_dong/com/raw/master/com/ipplus360/db/awdb/0.0.1-SNAPSHOT/awdb-0.0.1-SNAPSHOT.jar