이 예제에서, 우리는 어떻게 IP 주소를 사용하여 위치 (국가, 도시, 위도, 경도)를 발견하는 방법을 보여줍니다.
1. GeoLite 데이터베이스
를 MaxMind는 무료 GeoLite 데이터베이스 (위치에 IP 주소)를 제공합니다.
2. GeoLite 자바 예
IP 주소를 이용하여 위치를 찾을 수 있도록 GeoIP 클라이언트 자바 API를 사용하는 예.
GetLocationExample.java
package com.mkyong.analysis.location; import java.io.File; import java.io.IOException; import com.maxmind.geoip.Location; import com.maxmind.geoip.LookupService; import com.maxmind.geoip.regionName; import com.mkyong.analysis.location.mode.ServerLocation; public class GetLocationExample { public static void main(String[] args) { GetLocationExample obj = new GetLocationExample(); ServerLocation location = obj.getLocation("206.190.36.45"); System.out.println(location); } public ServerLocation getLocation(String ipAddress) { File file = new File( "C:\\resources\\location\\GeoLiteCity.dat"); return getLocation(ipAddress, file); } public ServerLocation getLocation(String ipAddress, File file) { ServerLocation serverLocation = null; try { serverLocation = new ServerLocation(); LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE); Location locationServices = lookup.getLocation(ipAddress); serverLocation.setCountryCode(locationServices.countryCode); serverLocation.setCountryName(locationServices.countryName); serverLocation.setRegion(locationServices.region); serverLocation.setRegionName(regionName.regionNameByCode( locationServices.countryCode, locationServices.region)); serverLocation.setCity(locationServices.city); serverLocation.setPostalCode(locationServices.postalCode); serverLocation.setLatitude(String.valueOf(locationServices.latitude)); serverLocation.setLongitude(String.valueOf(locationServices.longitude)); } catch (IOException e) { System.err.println(e.getMessage()); } return serverLocation; } }
Output
ServerLocation [countryCode=US, countryName=United States, region=CA, regionName=California, city=Sunnyvale, postalCode=94089, latitude=37.424896, longitude=-122.0074]
출처 - http://www.mkyong.com/java/java-find-location-using-ip-address/
댓글 없음:
댓글 쓰기