Skip to content

Commit c6a3ac2

Browse files
authored
Merge pull request #89 from Raxcl/feature-ip2region
升级 ip2region ip地址查询方法
2 parents c6f2818 + 169e68f commit c6a3ac2

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

blog-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
<dependency>
125125
<groupId>org.lionsoul</groupId>
126126
<artifactId>ip2region</artifactId>
127-
<version>1.7.2</version>
127+
<version>2.6.5</version>
128128
</dependency>
129129
<!-- 解析客户端操作系统、浏览器 -->
130130
<dependency>

blog-api/src/main/java/top/naccl/util/IpAddressUtils.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import lombok.extern.slf4j.Slf4j;
44
import org.apache.commons.lang3.StringUtils;
5-
import org.lionsoul.ip2region.DataBlock;
6-
import org.lionsoul.ip2region.DbConfig;
7-
import org.lionsoul.ip2region.DbSearcher;
8-
import org.lionsoul.ip2region.Util;
5+
import org.lionsoul.ip2region.xdb.Searcher;
96
import org.springframework.core.io.ClassPathResource;
107
import org.springframework.stereotype.Component;
118
import org.springframework.util.FileCopyUtils;
@@ -64,7 +61,7 @@ public static String getIpAddress(HttpServletRequest request) {
6461
return StringUtils.substringBefore(ip, ",");
6562
}
6663

67-
private static DbSearcher searcher;
64+
private static Searcher searcher;
6865
private static Method method;
6966

7067
/**
@@ -75,13 +72,13 @@ public static String getIpAddress(HttpServletRequest request) {
7572
*/
7673
@PostConstruct
7774
private void initIp2regionResource() throws Exception {
78-
InputStream inputStream = new ClassPathResource("/ipdb/ip2region.db").getInputStream();
75+
InputStream inputStream = new ClassPathResource("/ipdb/ip2region.xdb").getInputStream();
7976
//将 ip2region.db 转为 ByteArray
8077
byte[] dbBinStr = FileCopyUtils.copyToByteArray(inputStream);
81-
DbConfig dbConfig = new DbConfig();
82-
searcher = new DbSearcher(dbConfig, dbBinStr);
78+
// 2、使用上述的 dbBinStr 创建一个完全基于内存的查询对象。
79+
searcher = new Searcher(null, null, dbBinStr);
8380
//二进制方式初始化 DBSearcher,需要使用基于内存的查找算法 memorySearch
84-
method = searcher.getClass().getMethod("memorySearch", String.class);
81+
method = searcher.getClass().getMethod("search", String.class);
8582
}
8683

8784
/**
@@ -91,21 +88,22 @@ private void initIp2regionResource() throws Exception {
9188
* @return
9289
*/
9390
public static String getCityInfo(String ip) {
94-
if (ip == null || !Util.isIpAddress(ip)) {
95-
log.error("Error: Invalid ip address");
96-
return "";
97-
}
9891
try {
99-
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip);
100-
String ipInfo = dataBlock.getRegion();
92+
String ipInfo = (String) method.invoke(searcher, ip);
10193
if (!StringUtils.isEmpty(ipInfo)) {
10294
ipInfo = ipInfo.replace("|0", "");
10395
ipInfo = ipInfo.replace("0|", "");
104-
return ipInfo;
10596
}
97+
return ipInfo;
10698
} catch (Exception e) {
10799
log.error("getCityInfo exception:", e);
108100
}
109101
return "";
110102
}
103+
public static void main(String[] args) throws Exception {
104+
IpAddressUtils ipAddressUtils = new IpAddressUtils();
105+
ipAddressUtils.initIp2regionResource();
106+
String cityInfo = IpAddressUtils.getCityInfo("14.215.177.39");
107+
System.out.println(cityInfo);
108+
}
111109
}
10.6 MB
Binary file not shown.

0 commit comments

Comments
 (0)