标签:
Geolocation IP,基于IP查询的地理位置的意思,企业网站(打开即可链接),我们可以根据免费版的geoip dat离线ip数据文件去查询。
ip离线文件 (解压放到c盘即可) 下载项目所需jar包: 1 2
package com.arcsight.service; import com.maxmind.geoip.LookupService; import java.io.IOException; import java.net.InetAddress; /** * Created by shaoyongyang on 2015/7/6. */ public class IPSearchService { public static void main(String [] args) { try { //InetAddress ip = InetAddress.getByAddress("218.28.2.111".getBytes()); //System.out.println(ip); String sep = System.getProperty("file.separator"); String dir = "C://"; //GeoLiteCity.dat 在c盘的根目录 String dbfile = dir + sep + "GeoLiteCity.dat"; LookupService cl = new LookupService(dbfile,LookupService.GEOIP_MEMORY_CACHE); //System.out.println(cl.getCountry("218.28.2.111").getName()); InetAddress inetAddress = InetAddress.getByName("218.28.2.111"); //System.out.println(cl.getLocation(inetAddress)); System.out.println(cl.getLocation(inetAddress).countryCode); System.out.println(cl.getLocation(inetAddress).area_code); System.out.println(cl.getLocation(inetAddress).city); System.out.println(cl.getLocation(inetAddress).region); System.out.println(cl.getLocation(inetAddress).dma_code); System.out.println(cl.getLocation("218.28.2.111").latitude); System.out.println(cl.getLocation("218.28.2.111").longitude); cl.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("IO Exception"); } } }
上面的代码没什么好说的,无非就是获取一个Location对象。
无图言屌?无图言屌!!!
总结:没什么技术含量,无非就是jar包,javaer的特色。
标签:
原文地址:http://my.oschina.net/0x4ad/blog/475275