# [<img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/>](https://ipinfo.io/) IPinfo Java Client Library
[![License](http://img.shields.io/:license-apache-blue.svg)](LICENSE)
This is the official Java client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to lookup your own IP address, or get any of the following details for an IP:
- [IP geolocation data](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude and longitude)
- [ASN information](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting or company)
- [Company data](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)
- [Carrier details](https://ipinfo.io/ip-carrier-api) (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)
Check all the data we have for your IP address [here](https://ipinfo.io/what-is-my-ip).
### Getting Started
You'll need an IPinfo API access token, which you can get by singing up for a free account at [https://ipinfo.io/signup](https://ipinfo.io/signup).
The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see [https://ipinfo.io/pricing](https://ipinfo.io/pricing)
[Click here to view the Java SDK's API documentation](https://ipinfo.github.io/java/).
#### Installation
##### Maven
Add these values to your pom.xml file:
Dependency:
```xml
<dependencies>
<dependency>
<groupId>io.ipinfo</groupId>
<artifactId>ipinfo-api</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
```
#### Quick Start
##### IP Information
````java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;
public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.build();
try {
IPResponse response = ipInfo.lookupIP("8.8.8.8");
// Print out the hostname
System.out.println(response.getHostname());
} catch (RateLimitedException ex) {
// Handle rate limits here.
}
}
}
````
##### ASN Information
````java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;
public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.build();
try {
ASNResponse response = ipInfo.lookupASN("AS7922");
// Print out country name
System.out.println(response.getCountry());
} catch (RateLimitedException ex) {
// Handle rate limits here.
}
}
}
````
#### Errors
- `ErrorResponseException`: A runtime exception accessible through the
`ExecutionException` of a future. This exception signals that something went
wrong when mapping the API response to the wrapper. You probably can't
recover from this exception.
- `RateLimitedException` An exception signalling that you've been rate limited.
#### Caching
This library provides a very simple caching system accessible in `SimpleCache`.
Simple cache is an in memory caching system that resets every time you restart
your code.
If you prefer a different caching methodology, you may use the `Cache`
interface and implement your own caching system around your own infrastructure.
The default cache length is 1 day, this can be changed by calling the
SimpleCache constructor yourself.
```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;
public class Main {
public static void main(String... args) {
// 5 Day Cache
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setCache(new SimpleCache(Duration.ofDays(5)))
.build();
try {
IPResponse response = ipInfo.lookupIP("8.8.8.8");
// Print out the hostname
System.out.println(response.getHostname());
} catch (RateLimitedException ex) {
// Handle rate limits here.
}
}
}
```
#### Country Name Lookup
This library provides a system to lookup country names through ISO2 country
codes.
By default, this translation exists for English (United States). If you wish to
provide a different language mapping, just use the following system in the
builder:
```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;
public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setCountryFile(new File("path/to/file.json"))
.build();
try {
IPResponse response = ipInfo.lookupIP("8.8.8.8");
// Print out the country code
System.out.println(response.getCountryCode());
// Print out the country name
System.out.println(response.getCountryName());
} catch (RateLimitedException ex) {
// Handle rate limits here.
}
}
}
```
This file must follow the same layout as seen [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/en_US.json)
More language files can be found [here](https://country.io/data)
#### EU Country Lookup
This library provides a system to lookup if a country is a member of the European Union (EU) through
ISO2 country codes.
By default, [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json) is the file containing all the EU members.
If you wish to provide a different file, just use the following system in the builder:
```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;
public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setEUCountryFile(new File("path/to/file.json"))
.build();
try {
IPResponse response = ipInfo.lookupIP("8.8.8.8");
// Print out whether the country is a member of the EU
System.out.println(response.isEU());
} catch (RateLimitedException ex) {
// Handle rate limits here.
}
}
}
```
#### Internationalization
This library provides a system to lookup if a country is a member of the European Union (EU), emoji and unicode of country's flag, code and symbol of country's currency, public link to the country's flag image as an SVG and continent code and name through ISO2 country codes.
Following are the file that are loaded by default:
- [eu.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
- [flags.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/flags.json)
- [currency.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/currency.json)
- [continent.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/continent.json)
If you wish to provide a different file, just use the following system in the builder:
```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;
public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setEUCountryFile(new File("path/to/file.json
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
这是 IPinfo.io IP 地址 API 的官方 Java 客户端库,允许您查找自己的 IP 地址,或获取 IP 的以下任何详细信息: IP地理定位数据(城市、地区、国家、邮政编码、经纬度) ASN 信息(ISP 或网络运营商、关联域名和类型,例如企业、托管或公司) 公司数据(使用 IP 地址的企业名称和域名) 运营商详细信息(移动运营商的名称以及该运营商的 MNC 和 MCC,如果 IP 专门用于移动流量) 在此查看我们掌握的有关您 IP 地址的所有数据。
资源推荐
资源详情
资源评论
收起资源包目录
java-master (3).zip (38个子文件)
java-master
pom.xml 6KB
src
test
java
io
ipinfo
IPinfoTest.java 13KB
main
resources
flags.json 15KB
en_US.json 5KB
eu.json 137B
continent.json 11KB
currency.json 11KB
java
io
ipinfo
api
cache
NoCache.java 313B
SimpleCache.java 1KB
Cache.java 666B
IPinfo.java 20KB
errors
RateLimitedException.java 244B
ErrorResponseException.java 211B
context
Context.java 2KB
request
ASNRequest.java 1KB
MapRequest.java 1KB
IPRequest.java 7KB
BaseRequest.java 1KB
model
Company.java 754B
CountryCurrency.java 593B
CountryFlag.java 597B
Prefix.java 2KB
Abuse.java 1KB
Carrier.java 722B
Continent.java 559B
MapResponse.java 614B
Domains.java 627B
ASN.java 1KB
IPResponse.java 5KB
ASNResponse.java 3KB
Privacy.java 1KB
LICENSE 11KB
CHANGELOG.md 1KB
.gitignore 21B
README.md 11KB
scripts
ctags.sh 768B
deploy.sh 133B
deploy_docs.sh 306B
共 38 条
- 1
资源评论
智达教育
- 粉丝: 2w+
- 资源: 279
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功