前文:
- 通过解析IP地址,返回该IP地址对应国名、身份/一级行政区、地级市/二级行政区、网络运营商(移动、联通、电信)。
效果图如下:
例子如下:
- 导入文件:ip2region.db(存放城市信息的数据文件)
- 调用例子:
public static void main(String[] args) {
String ipAddress = "IP地址";
AttributionVo detail = IPUtil.getCityInfo(ipAddress);
System.out.println("输入的IP为:" + ipAddress + ",返回的结果为:" +detail);
}
package com.alex.examples.entity;
import com.alex.examples.vo.AttributionVo;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.springframework.util.ResourceUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
public class IPUtil {
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if ("0:0:0:0:0:0:0:1".equals(ip)) {
ip = "127.0.0.1";
}
if (ip.split(",").length > 1) {
ip = ip.split(",")[0];
}
return ip;
}
public static AttributionVo getCityInfo(String ip) {
AttributionVo vo = new AttributionVo();
File file = null;
try {
Integer systemType = isSystemType();
if (systemType == 0) {
file = ResourceUtils.getFile("classpath:ip2region.db");
}
if (systemType == 1) {
file = ResourceUtils.getFile("/home/caixiaojiong/jar/ip2region.db");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (!file.exists()) {
System.out.println("Error: Invalid ip2region.db file, filePath:" + file.getPath());
return vo;
}
int algorithm = DbSearcher.BTREE_ALGORITHM;
try {
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, file.getPath());
Method method;
switch (algorithm) {
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
default:
return null;
}
DataBlock dataBlock;
if (!Util.isIpAddress(ip)) {
System.out.println("Error: Invalid ip address");
return vo;
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
String[] result = dataBlock.getRegion().split("\\|");
vo.setNationName(result[0]);
vo.setProvince(result[2]);
vo.setCity(result[3]);
vo.setOperator(result[4]);
return vo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static boolean isLinux() {
return System.getProperty("os.name").toLowerCase().contains("linux");
}
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
public static Integer isSystemType() {
Integer isSystemType = 0;
boolean isWindows = isWindows();
if (isWindows) {
isSystemType = 0;
return isSystemType;
}
boolean isLinux = isLinux();
if (isLinux) {
isSystemType = 1;
return isSystemType;
}
return isSystemType;
}
}