JAVA - IP manzil yordamida joylashuvni aniqlash
Maqsad: IP manzil yordamida, joylashuvni(davlat, shahar, latitude, longitude) aniqlash dasturi.
Kim uchun mo'ljallangan: JAVA dasturlash tilidan xabardor bo'lganlar
Ushbu dastur offline rejimda ham ishlaydi, ya'ni IP manzillar orqali joylashuvni aniqlashda, MixMand sayti taqdim qilgan GeoLite ma’lumotlar bazasi va API lardan foydalanamiz.
GeoIP Java API .jar faylni proektga qo’shing.
ServerLocation.java
GetLocationExample.java
Natija:
IP=195.158.2.220
IP=64.17.254.216
Endi, yuqoridagi latitude va longitude lardan foydalanib google mapsdan ushbu nuqtani ko’rishimiz mumkin. Buning uchun, google maps qidiruv oynasiga 34.0522,-118.2437(latitude,longitude) qiymatlarini kiriting:
Nasib qilsa, keyingi maqolalarda bu dasturni PHP da ham ko'rib chiqamiz.
Qo'shimcha:
IP manzilni aniqlash:
Natija:
Ko'rib turganingizdek, bu yerda lokal IP manzili chiqdi. Chunki buni qurilmadan olyapdi, agar shlyuz orqali internetga chiqayotgan bo'lsak, va o'sha internetga chiqish IP sini aniqlamoqchi bo'lsangiz unda quidagi yo'llar orqali amalaga oshirish mumkin:
1) web-serverda ishlaydigan skript(php, jsp yoki asp da) yozib, kelgan so'rovning IP manzilni qaytarish mumkin. Agar JSP da yozsangiz:
2) Yoki tayyor web-servislardan foydalanish ham mumkin, va kelgan qiymatni parse qilib IP manzilni aniqlab olish mumkin.
Misol:
Natija:
Kim uchun mo'ljallangan: JAVA dasturlash tilidan xabardor bo'lganlar
Ushbu dastur offline rejimda ham ishlaydi, ya'ni IP manzillar orqali joylashuvni aniqlashda, MixMand sayti taqdim qilgan GeoLite ma’lumotlar bazasi va API lardan foydalanamiz.
- GeoLite City bazasini tanlang va binary ko’rinishida ko’chirib oling. City bazasida shaharlar ham kiritilgan, lekin bu bazada hamma shaharlar ham mavjud emas.
- GeoIP klient JAVA APIni .jar faylini ko’chirib oling.
- Ishlatigan platforma: Eclipse
GeoIP Java API .jar faylni proektga qo’shing.
ServerLocation.java
public class ServerLocation {
private String countryCode;
private String countryName;
private String region;
private String regionName;
private String city;
private String postalCode;
private String latitude;
private String longitude;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String string) {
this.latitude = string;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String string) {
this.longitude = string;
}
public String toString() {
return "ServerLocation [countryCode=" + countryCode + ", countryName="+ countryName + ", region=" + region + ", regionName="+ regionName + ", city=" + city + ", postalCode=" + postalCode+", latitude=" + latitude + ", longitude=" + longitude + "]";
}
}
GetLocationExample.java
import java.io.File;
import java.io.IOException;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
public class GetLocationExample {
public static void main(String[] args) {
// GetLocationExample class obyekti yaratilyapdi
GetLocationExample obj = new GetLocationExample();
// ServerLocation obyekti
ServerLocation location = obj.getLocation("195.158.2.220");
System.out.println(location);
}
public ServerLocation getLocation(String ipAddress) {
// baza fayli chaqirilyapdi
File file = new File("GeoLiteCity.dat");
// ip manzil va fayl obyektni getLocation() metodga uzatlyapdi
return getLocation(ipAddress, file);
}
public ServerLocation getLocation(String ipAddress, File file) {
ServerLocation serverLocation = null;
try {
serverLocation = new ServerLocation();
// LookipService klassi berilgan IP manzil asosida ma'lumot qidirish vazifasini bajaradi.
LookupService lookup = new LookupService(file,
LookupService.GEOIP_MEMORY_CACHE);
// getLocation() metodga ip jo'natiladi, Location qaytadi
Location locationServices = lookup.getLocation(ipAddress);
// serverLocation obyektga kerakli ma'lumotlar saqlanadi.
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;
}
}
Natija:
IP=195.158.2.220
ServerLocation [countryCode=UZ, countryName=Uzbekistan, region=null, regionName=null, city=null, postalCode=null, latitude=41.0, longitude=64.0]
IP=64.17.254.216
ServerLocation [countryCode=US, countryName=United States, region=CA, regionName=California, city=Los Angeles, postalCode=null, latitude=34.0522, longitude=-118.2437]
Endi, yuqoridagi latitude va longitude lardan foydalanib google mapsdan ushbu nuqtani ko’rishimiz mumkin. Buning uchun, google maps qidiruv oynasiga 34.0522,-118.2437(latitude,longitude) qiymatlarini kiriting:
Nasib qilsa, keyingi maqolalarda bu dasturni PHP da ham ko'rib chiqamiz.
Qo'shimcha:
IP manzilni aniqlash:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpAddress {
public static void main(String[] args) {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Joriy ip: "+ ip.getHostAddress());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Natija:
Joriy ip: 172.20.15.113
Ko'rib turganingizdek, bu yerda lokal IP manzili chiqdi. Chunki buni qurilmadan olyapdi, agar shlyuz orqali internetga chiqayotgan bo'lsak, va o'sha internetga chiqish IP sini aniqlamoqchi bo'lsangiz unda quidagi yo'llar orqali amalaga oshirish mumkin:
1) web-serverda ishlaydigan skript(php, jsp yoki asp da) yozib, kelgan so'rovning IP manzilni qaytarish mumkin. Agar JSP da yozsangiz:
request.getRemoteAddr()
2) Yoki tayyor web-servislardan foydalanish ham mumkin, va kelgan qiymatni parse qilib IP manzilni aniqlab olish mumkin.
Misol:
URL whatismyip = new URL("http://api.externalip.net/ip/"); // http://api.externalip.net/ip/ - tashqi IP ni aniqlab beruvchi xizmat
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
String ipp = in.readLine(); //ip manzil string ko'rinishida olinyapdi
System.out.println("Tashqi IP:"+ipp);
Natija:
Tashqi IP:195.158.2.220
bugun bitta ip shaxarni (yo viloyatni yo umuman mamlakatni) u burchagida bulsa, ertaga boshqa burchagida bulsa bundan mano yuq :)
bu narsa manimcha europa amerika uchun ishlaydi
bizda hatto real ip mavjud emas asosiy providerda :)
Bu yerda manzil bo'yicha uning lat/long dagi koordinatalari aniqlanadi, api ni o'rganib chiqqan holda undan foydalansangiz bo'ladi.
maps.google.com/maps/api/geocode/json?address=kocha,shaxar,davlat&sensor=true ko'rinishida so'rov yuboriladi.
Batafsil: developers.google.com/maps/documentation/geocoding/