【geoip】geoip操作GeoLiteCity.dat

如何下载GeoLiteCity.dat.gz

下载:wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

或 https://download.csdn.net/download/michaeljy1991/9373860

解压:gunzip GeoLiteCity.dat.gz

 

jiayi@ubuntu:~$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 
--2015-12-22 18:10:24--  http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Resolving geolite.maxmind.com (geolite.maxmind.com)... 141.101.114.190
Connecting to geolite.maxmind.com (geolite.maxmind.com)|141.101.114.190|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12328291 (12M) [application/octet-stream]
Saving to: ‘GeoLiteCity.dat.gz’

100%[===================================================================================================================>] 12,328,291   739KB/s   in 23s    

2015-12-22 18:10:54 (530 KB/s) - ‘GeoLiteCity.dat.gz’ saved [12328291/12328291]

jiayi@ubuntu:~$ ls
Desktop  Documents  Downloads  examples.desktop  GeoLiteCity.dat.gz  Music  Pictures  Public  soft  Templates  tool  Vide

 

 

 

使用Geoip操作GeoLiteCity.dat

TEST

 

public class IPSearchService {
    public static void main(String[] args) throws IOException {
        String geoLiteCityFilePath = Thread.currentThread().getContextClassLoader().getResource("GeoLiteCity.dat").getPath();
        LookupService lookupService = new LookupService(geoLiteCityFilePath,LookupService.GEOIP_MEMORY_CACHE);
        String ip = "218.28.2.111";
        InetAddress inetAddress = InetAddress.getByName(ip);
        System.out.println("areaCode:"+lookupService.getLocation(inetAddress).area_code);
        System.out.println("city:"+lookupService.getLocation(inetAddress).city);
        System.out.println("countryCode:"+lookupService.getLocation(inetAddress).countryCode);
        System.out.println("countryName:"+lookupService.getLocation(inetAddress).countryName);
        System.out.println("postalCode:"+lookupService.getLocation(inetAddress).postalCode);
        System.out.println("region:"+lookupService.getLocation(inetAddress).region);
        System.out.println("dma_code:"+lookupService.getLocation(inetAddress).dma_code);
        System.out.println("latitude:"+lookupService.getLocation(inetAddress).latitude);
        System.out.println("longitude:"+lookupService.getLocation(inetAddress).longitude);
        System.out.println("metro_code:"+lookupService.getLocation(inetAddress).metro_code);
    }
}

结果:

 

 

areaCode:0
city:Zhengzhou
countryCode:CN
countryName:China
postalCode:null
region:09
dma_code:0
latitude:34.683594
longitude:113.5325
metro_code:0

 

 

 

POM

 

 <properties>
        <geoip-api.version>1.2.15</geoip-api.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.maxmind.geoip</groupId>
            <artifactId>geoip-api</artifactId>
            <version>${geoip-api.version}</version>
        </dependency>
    </dependencies>

 

 

 

PS:可以将离线数据GeoLiteCity.dat读到redis里面,然后通过jedis操作,也许是更好的办法。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值