Java - 通过IP地址获取用户所在地 (三)

2014-11-24 11:07:16 · 作者: · 浏览: 4
";
LookupService cl = new LookupService(dbfile,
LookupService.GEOIP_MEMORY_CACHE);
Location location = cl.getLocation(ipaddr);

cl.close();
return location;
}
}

public class GeoBusiness {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println(getLocationByIp("220.181.111.147").getCity());
System.out.println(getLocationByIp("220.181.111.147").getCountryName());
}

public static Location getLocationByIp(String ipaddr) throws IOException {
String sep = System.getProperty("file.separator");
String dir = Play.configuration.getProperty("geoip.datdir");
String dbfile = dir + sep + "GeoLiteCity.dat";
LookupService cl = new LookupService(dbfile,
LookupService.GEOIP_MEMORY_CACHE);
Location location = cl.getLocation(ipaddr);

cl.close();
return location;
}
}
更详细的功能比如获取省,经纬度可以参考我最前面给出的网址。