【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

 

 
        1.2.15
    

    
        
            com.maxmind.geoip
            geoip-api
            ${geoip-api.version}
        
    

 

 

 

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

你可能感兴趣的:(geoip)