JAVA根据ip地址获取归属地的实现方法
作者:旺崽刘奶
本文主要介绍了JAVA根据ip地址获取归属地的实现方法,要通过Java程序获取IP地址对应的城市,需要借助第三方的IP地址库,下面就来介绍一下,感兴趣的可以了解一下
IP获取归属地
1.通过地址库获取
如果使用API接口获取,可能会出现服务挂了,或者服务地址不提供服务了等问题。而采用本地地址库就没有这些问题。
本文采用离线IP地址定位库 Ip2region,Ip2region是一个离线IP地址定位库,微秒的查询时间:
实现步骤:
访问官网github地址:https://github.com/lionsoul2014/ip2region

找到data目录下的:ip2region.xdb文件下载下来

把ip2region.xdb文件放在resources目录下

在模块中引入maven依赖
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>2.6.5</version> </dependency>
获取归属地:
private Searcher searcher;
@Override
public String getIpAddress(String ip){
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域网ip";
}
if (searcher == null) {
try {
File file = ResourceUtils.getFile("classpath:db/data_ip2region.xdb");
String dbPath = file.getPath();
searcher = Searcher.newWithFileOnly(dbPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String region = null;
String errorMessage = null;
try {
region = searcher.search(ip);
} catch (Exception e) {
errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.length() > 256) {
errorMessage = errorMessage.substring(0, 256);
}
e.printStackTrace();
}
// 输出 region
System.out.println(region);
return region;
}
这里Searcher引用的是仓库里面的检索方法,到这里就完成了可以获取到ip对应的归属地。
调用方法后运行结果如下

注意!!!! 本地是没问题的 如果打成jar包放在linux服务器上会读取不到
解决办法:
public String getIpCity(String ip) {
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域网ip";
}
if (searcher == null) {
try {
//本地环境需要加上 classpath:
// File file = ResourceUtils.getFile("db/data_ip2region.xdb");
// String dbPath = file.getPath();
// searcher = Searcher.newWithFileOnly(dbPath);
//这里通过流获取 解决jar包无法读取文件问题
ResponseEntity<byte[]> test = test("db/data_ip2region.xdb");
searcher = Searcher.newWithBuffer(test.getBody());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String region = null;
String errorMessage = null;
try {
region = searcher.search(ip);
} catch (Exception e) {
errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.length() > 256) {
errorMessage = errorMessage.substring(0, 256);
}
e.printStackTrace();
}
// 输出 region
return region;
}
public static ResponseEntity<byte[]> test(String templateName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(templateName);
String filename = classPathResource.getFilename();
@Cleanup InputStream inputStream = classPathResource.getInputStream();
byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
String fileName = new String(filename.getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱码问题
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
}至于什么是Ip2region 官网上面有介绍这里就不多介绍了
到此这篇关于JAVA根据ip地址获取归属地的实现方法的文章就介绍到这了,更多相关JAVA ip地址获取归属地内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
