在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:maxmind/GeoIP2-java开源软件地址:https://github.com/maxmind/GeoIP2-java开源编程语言:Java 98.7%开源软件介绍:GeoIP2 Java APIDescriptionThis distribution provides an API for the GeoIP2 and GeoLite2 web services and databases. InstallationMavenWe recommend installing this package with Maven. To do this, add the dependency to your pom.xml: <dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>3.0.1</version>
</dependency> GradleAdd the following to your
JAR FilesIf you are unable to use Maven or Gradle, you may include the IP Geolocation UsageIP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household. Web Service UsageTo use the web service API, you must create a new If the request succeeds, the method call will return a model class for the end point you called. This model in turn contains multiple record classes, each of which represents part of the data returned by the web service. If the request fails, the client class throws an exception. The See the API documentation for more details. Web Service ExampleCountry Service// This creates a WebServiceClient object that is thread-safe and can be
// reused across requests. Reusing the object will allow it to keep
// connections alive for future requests.
//
// Replace "42" with your account ID and "license_key" with your license key.
// To use the GeoLite2 web service instead of the GeoIP2 web service, call the
// host method on the builder with "geolite.info", e.g.
// new WebServiceClient.Builder(42, "license_key").host("geolite.info").build()
WebServiceClient client = new WebServiceClient.Builder(42, "license_key")
.build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
// Do the lookup
CountryResponse response = client.country(ipAddress);
Country country = response.getCountry();
System.out.println(country.getIsoCode()); // 'US'
System.out.println(country.getName()); // 'United States'
System.out.println(country.getNames().get("zh-CN")); // '美国' City Plus Service// This creates a WebServiceClient object that is thread-safe and can be
// reused across requests. Reusing the object will allow it to keep
// connections alive for future requests.
//
// Replace "42" with your account ID and "license_key" with your license key.
// To use the GeoLite2 web service instead of the GeoIP2 web service, call the
// host method on the builder with "geolite.info", e.g.
// new WebServiceClient.Builder(42, "license_key").host("geolite.info").build()
WebServiceClient client = new WebServiceClient.Builder(42, "license_key")
.build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
// Do the lookup
CityResponse response = client.city(ipAddress);
Country country = response.getCountry();
System.out.println(country.getIsoCode()); // 'US'
System.out.println(country.getName()); // 'United States'
System.out.println(country.getNames().get("zh-CN")); // '美国'
Subdivision subdivision = response.getMostSpecificSubdivision();
System.out.println(subdivision.getName()); // 'Minnesota'
System.out.println(subdivision.getIsoCode()); // 'MN'
City city = response.getCity();
System.out.println(city.getName()); // 'Minneapolis'
Postal postal = response.getPostal();
System.out.println(postal.getCode()); // '55455'
Location location = response.getLocation();
System.out.println(location.getLatitude()); // 44.9733
System.out.println(location.getLongitude()); // -93.2323 Insights Service// This creates a WebServiceClient object that is thread-safe and can be
// reused across requests. Reusing the object will allow it to keep
// connections alive for future requests.
//
// Replace "42" with your account ID and "license_key" with your license key.
// Please note that the GeoLite2 web service does not support Insights.
WebServiceClient client = new WebServiceClient.Builder(42, "license_key")
.build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
// Do the lookup
InsightsResponse response = client.insights(ipAddress);
Country country = response.getCountry();
System.out.println(country.getIsoCode()); // 'US'
System.out.println(country.getName()); // 'United States'
System.out.println(country.getNames().get("zh-CN")); // '美国'
System.out.println(country.getConfidence()); // 99
Subdivision subdivision = response.getMostSpecificSubdivision();
System.out.println(subdivision.getName()); // 'Minnesota'
System.out.println(subdivision.getIsoCode()); // 'MN'
System.out.println(subdivision.getConfidence()); // 90
City city = response.getCity();
System.out.println(city.getName()); // 'Minneapolis'
System.out.println(city.getConfidence()); // 50
Postal postal = response.getPostal();
System.out.println(postal.getCode()); // '55455'
System.out.println(postal.getConfidence()); // 40
Location location = response.getLocation();
System.out.println(location.getLatitude()); // 44.9733
System.out.println(location.getLongitude()); // -93.2323
System.out.println(location.getAccuracyRadius()); // 3
System.out.println(location.getTimeZone()); // 'America/Chicago'
System.out.println(response.getTraits().getUserType()); // 'college' Database UsageTo use the database API, you must create a new After you have created the If the lookup succeeds, the method call will return a response class for the GeoIP2 lookup. The class in turn contains multiple record classes, each of which represents part of the data returned by the database. We recommend reusing the See the API documentation for more details. CachingThe database API supports pluggable caching (by default, no caching is
performed). A simple implementation is provided by Usage: new DatabaseReader.Builder(file).withCache(new CHMCache()).build(); Packaging Database in a JARIf you are packaging the database file as a resource in a JAR file using
Maven, you must
disable binary file filtering.
Failure to do so will result in Database ExampleCity// A File object pointing to your GeoIP2 or GeoLite2 database
File database = new File("/path/to/GeoIP2-City.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
// Replace "city" with the appropriate method for your database, e.g.,
// "country".
CityResponse response = reader.city(ipAddress);
Country country = response.getCountry();
System.out.println(country.getIsoCode()); // 'US'
System.out.println(country.getName()); // 'United States'
System.out.println(country.getNames().get("zh-CN")); // '美国'
Subdivision subdivision = response.getMostSpecificSubdivision();
System.out.println(subdivision.getName()); // 'Minnesota'
System.out.println(subdivision.getIsoCode()); // 'MN'
City city = response.getCity();
System.out.println(city.getName()); // 'Minneapolis'
Postal postal = response.getPostal();
System.out.println(postal.getCode()); // '55455'
Location location = response.getLocation();
System.out.println(location.getLatitude()); // 44.9733
System.out.println(location.getLongitude()); // -93.2323 Anonymous IP// A File object pointing to your GeoIP2 Anonymous IP database
File database = new File("/path/to/GeoIP2-Anonymous-IP.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
InetAddress ipAddress = InetAddress.getByName("85.25.43.84");
AnonymousIpResponse response = reader.anonymousIp(ipAddress);
System.out.println(response.isAnonymous()); // true
System.out.println(response.isAnonymousVpn()); // false
System.out.println(response.isHostingProvider()); // false
System.out.println(response.isPublicProxy()); // false
System.out.println(response.isResidentialProxy()); // false
System.out.println(response.isTorExitNode()); //true
} ASN// A File object pointing to your GeoLite2 ASN database
File database = new File("/path/to/GeoLite2-ASN.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
AsnResponse response = reader.asn(ipAddress);
System.out.println(response.getAutonomousSystemNumber()); // 217
System.out.println(response.getAutonomousSystemOrganization()); // 'University of Minnesota'
} Connection-Type// A File object pointing to your GeoIP2 Connection-Type database
File database = new File("/path/to/GeoIP2-Connection-Type.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
ConnectionTypeResponse response = reader.connectionType(ipAddress);
// getConnectionType() returns a ConnectionType enum
ConnectionType type = response.getConnectionType();
System.out.println(type); // 'Corporate' Domain// A File object pointing to your GeoIP2 Domain database
File database = new File("/path/to/GeoIP2-Domain.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
DomainResponse response = reader.domain(ipAddress);
System.out.println(response.getDomain()); // 'Corporate' Enterprise// A File object pointing to your GeoIP2 Enterprise database
File database = new File("/path/to/GeoIP2-Enterprise.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
// Use the enterprise(ip) method to do a lookup in the Enterprise database
EnterpriseResponse response = reader.enterprise(ipAddress);
Country country = response.getCountry();
System.out.println(country.getIsoCode()); // 'US'
System.out.println(country.getName()); // 'United States'
System.out.println(country.getNames().get("zh-CN")); // '美国'
System.out.println(country.getConfidence()); // 99
Subdivision subdivision = response.getMostSpecificSubdivision();
System.out.println(subdivision.getName()); // 'Minnesota'
System.out.println(subdivision.getIsoCode()); // 'MN'
System.out.println(subdivision.getConfidence()); // 77
City city = response.getCity();
System.out.println(city.getName()); // 'Minneapolis'
System.out.println(city.getConfidence()); // 11
Postal postal = response.getPostal();
System.out.println(postal.getCode()); // '55455'
System.out.println(postal.getConfidence()); // 5
Location location = response.getLocation();
System.out.println(location.getLatitude()); // 44.9733
System.out.println(location.getLongitude()); // -93.2323
System.out.println(location.getAccuracyRadius()); // 50
} ISP// A File object pointing to your GeoIP2 ISP database
File database = new File("/path/to/GeoIP2-ISP.mmdb");
// This creates the DatabaseReader object. To improve performance, reuse
// the object across lookups. The object is thread-safe.
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
IspResponse response = reader.isp(ipAddress);
System.out.println(response.getAutonomousSystemNumber()); // 217
System.out.println(response.getAutonomousSystemOrganization()); // 'University of Minnesota'
System.out.println(response.getIsp()); // 'University of Minnesota'
System.out.println(response.getOrganization()); // 'University of Minnesota' ExceptionsFor details on the possible errors returned by the web service itself, see the GeoIP2 web service documentation. If the web service returns an explicit erro |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论