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.

  1. GeoLite City bazasini tanlang va binary ko’rinishida ko’chirib oling. City bazasida shaharlar ham kiritilgan, lekin bu bazada hamma shaharlar ham mavjud emas.
  2. GeoIP klient JAVA APIni .jar faylini ko’chirib oling.
  3. Ishlatigan platforma: Eclipse
Kerakli fayllarni ko’chirib bo’lgandan so’ng, yangi Eclipse da yangi proekt yarating. Src papkadan tashqariga ma’lumotlar bazasi fayli “GeoLiteCity.dat” faylini ko’chiring.


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

10 комментариев

mageUz
IP adressni aniqlash bo'yicha ham 3-4 qator kod qo'shib ko'rsa bo'lar ekan, funksional yaxlit bo'ladi shunda.
0
sarvar_uz
maqolaga IP manzilni aniqlash bo'yicha qo'shimcha kiritdim.
0
geniuz
ip dynamic bulsachi :)
0
sarvar_uz
dinamik ip bo'lsa ham topadi, ip diapazon bo'yicha, agar bazada mavjud bolsa
0
geniuz
topadimi demoqchimasman :)
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 :)
1
DaaarK
Menga ham shunga o'xshash baza kerak edi,faqat Toshkent ko'chalariniki ya'ni ko'chalarning latitude va longitude lari kerak qanday maslahat berasizlar
0
mageUz
0
DaaarK
To'g'risi siz bergan url ishlamadi lekn baribr rahmat
0
mageUz
Men Googlening manzillar API sidan foydalaning demoqchi edim. Berilgan url uyidagi natijani ko'rsatadi:
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Temur Malik Street",
               "short_name" : "Temur Malik Street",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Tashkent",
               "short_name" : "Tashkent",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Tashkent Province",
               "short_name" : "Tashkent Province",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Uzbekistan",
               "short_name" : "UZ",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Temur Malik Street, Tashkent, Uzbekistan",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.3562537,
                  "lng" : 69.3790135
               },
               "southwest" : {
                  "lat" : 41.303641,
                  "lng" : 69.3400839
               }
            },
            "location" : {
               "lat" : 41.3242506,
               "lng" : 69.3662944
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.3562537,
                  "lng" : 69.3790135
               },
               "southwest" : {
                  "lat" : 41.303641,
                  "lng" : 69.3400839
               }
            }
         },
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}


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/
0