springboot整合IPDB,获取地区信息
前言
最近在做用户活跃度的项目,项目中需要统计全国各个区域的ip数,浏览量等,这里需要用到ipdb数据库,根据浏览的ip获取到地区(省市)。其实很简单,只需要在项目导入ipdb的jar包,再引入ipdb文件,重点是ipdb文件,ipdb无法上传,需要的可以联系我。
环境
springboot1.5.x
ipdb文档参考:ipdb参考文档
pom文件
<dependency><groupId>net.ipip</groupId><artifactId>ipdb</artifactId><version>1.1.3</version></dependency>
yml文件
# 数据库连接我就不放了
file:save-path: /usr/share/active/files
service层代码
@Service
public class TestServiceImpl {@Value("${file.save-path}")private String filePath;public CityInfo findCityByIp() throws IPFormatException, IOException{// ipdb文件位置String fileName = filePath + "/ipdb/ipipfree.ipdb";City db = new City(fileName);// 将整形表示的ip地址转换为字符串表示,ip可以从业务逻辑中获取, Long.parseLong(ip)String ipStr = longToIp(37226631L);// findInfo方法中"CN"表示中国,返回是的city实体CityInfo info = db.findInfo(ipStr, "CN");System.out.println(info);return info;
}/*** 将整形表示的ip地址转换为字符串表示.** @param ip 32位整数表示的ip地址* @return 点分式表示的ip地址*/public static final String longToIp(long ip) {final long[] mask = {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000};final StringBuilder ipAddress = new StringBuilder();for (int i = 0; i < mask.length; i++) {ipAddress.insert(0, (ip & mask[i]) >> (i * 8));if (i < mask.length - 1) {ipAddress.insert(0, ".");}}return ipAddress.toString();}
}
测试用例
package com.test;import com.test.service.InfluxDataServiceImpl;
import net.ipip.ipdb.CityInfo;
import net.ipip.ipdb.IPFormatException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;import javax.annotation.Resource;
import java.io.IOException;@SpringBootTest(classes = ActiveApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ActiveApplicationTests {@Resourceprivate TestServiceImpl testService;@Testpublic void testSend() throws IPFormatException, IOException {testService.findCityByIp();}
}
测试结果
country_name:中国
region_name:河南
city_name:郑州
owner_domain:
isp_domain:
latitude:
longitude:
timezone:
utc_offset:
china_admin_code:
idd_code:
country_code:
continent_code:
idc:
base_station:
country_code3:
european_union:
currency_code:
currency_name:
anycast:
springboot整合IPDB,获取地区信息相关推荐
- SpringBoot整合redisTemplate获取自增主键
SpringBoot整合redisTemplate获取自增主键 在Spring Boot应用程序中,我们可以使用Redis作为缓存或数据存储.当我们使用Redis存储数据时,我们可能需要使用自增主键. ...
- mysql 纯真ip 导出dat_C# 读取纯真IP数据库QQWry.dat获取地区信息
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace 纯真IP数据 ...
- springboot整合swagger+mybatisplus案例
1.前后端分离的一个常用的文档接口swaggerui越来越受欢迎,方便了前端以及后端人员的测试 2.如下为springboot整合swagger和mybatispus案例的github地址:https ...
- 租房项目 获取地区信息服务
流程 接口 # Request: method: GET url: api/v1.0/areas # data: no input data# Response: # 返回成功 {"errn ...
- SpringBoot整合Encahce后,访问页面页面出现异常信息,无法获取返回数据
问题描述: 使用SpringBoot整合Encache时,页面出现异常并显示以下信息: Property or field 'xxx' cannot be found on object of typ ...
- SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)
SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 目录 SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 1.概述 2.最佳实践 2.1创建项目引入依赖(mail) ...
- 实践:nginx代理,通过使用GeoIp模块获取访问者IP及访问地区信息
目录 前言 一.在Linux中创建存放nginx模块文件夹 存在就不需要再次创建. 将所有nginx额外模块包都放到这个目录下 二.配置host文件代理,避免访问github太慢(已配置了跳过) 三. ...
- Android 获取定位权限,获取位置信息(国家、地区、经纬)
第一步 授权 在 AndroidManifest.xml 中加上位置信息权限按需要添加 <uses-permission android:name="android.permissio ...
- vue项目中获取当前设备 操作系统及版本,设备型号,ip,地区信息
1.获取ip,地区信息 使用搜狐IP地址查询接口(?后边设置编码)须连网! http://pv.sohu.com/cityjson http://pv.sohu.com/cityjson?ie=utf ...
最新文章
- 算法面试的理想与现实
- IE8 CSS hack
- 使用async await 封装 axios
- Jquery工作常用实例——隐藏功能实现
- mysql和hive的sql语句,hive中使用sql语句需要注意的事项
- android dbflow教程,Android高性能ORM数据库DBFlow入门教程
- MutationObserver详解
- 开源项目贡献者_我如何从一名贡献者转变为一个开源项目维护者
- java中Comparable实现对象的比较
- windbg-内存破坏实例分析
- 动态数据源切换--AbstractRoutingDataSource
- 使用idea练习springmvc时,出现404错误总结
- 计算机添加pdf打印机驱动,给win7系统电脑安装PDF虚拟打印机的详细教程
- ES6的新特性,前端必看知识点
- 第三阶段应用层——1.11 数码相册—setting_page设置页面的显存管理、页面规划、输入控制
- 《功夫熊猫》的人生启示
- 关于网络渗透的过程以及感想记录
- 植树节的微信软文如何写?素材加文案帮你分分钟搞定!
- oracle显示人民币,如何在ORACLE中实现人民币大写的转换
- Python标准库:一些受欢迎的模块