Java查询IP归属地

引言

在网络应用开发中,经常需要获取用户的IP地址,并进一步获取该IP地址的归属地信息。Java提供了多种方法来实现这一功能,本文将介绍常用的几种方法,并通过代码示例演示。

方法一:使用第三方接口查询

可以利用第三方接口来查询IP地址的归属地信息。常用的接口有新浪、淘宝等,这些接口通常以JSON或XML格式返回查询结果。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;

public class IPQuery {
    public static void main(String[] args) {
        String ip = "127.0.0.1";
        String urlStr = " + ip;

        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);

            StringBuilder sb = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();

            JSONObject jsonObject = JSONObject.parseObject(sb.toString());
            JSONObject data = jsonObject.getJSONObject("data");
            String country = data.getString("country");
            String region = data.getString("region");
            String city = data.getString("city");
            String isp = data.getString("isp");

            System.out.println("IP地址:" + ip);
            System.out.println("所在国家:" + country);
            System.out.println("所在省份:" + region);
            System.out.println("所在城市:" + city);
            System.out.println("所属运营商:" + isp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

方法二:使用IP数据库查询

另一种常用的方法是使用IP数据库,将IP地址与归属地信息建立映射关系,然后根据IP地址查询对应的归属地信息。常用的IP数据库有纯真IP库、GeoIP等。

import java.io.IOException;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Subdivision;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Postal;
import com.maxmind.geoip2.record.Location;
import java.io.File;
import java.net.InetAddress;

public class IPQuery {
    public static void main(String[] args) {
        String ip = "127.0.0.1";
        String dbPath = "GeoLite2-City.mmdb";

        try {
            File database = new File(dbPath);
            DatabaseReader reader = new DatabaseReader.Builder(database).build();
            InetAddress ipAddress = InetAddress.getByName(ip);
            CityResponse response = reader.city(ipAddress);

            Country country = response.getCountry();
            Subdivision subdivision = response.getMostSpecificSubdivision();
            City city = response.getCity();
            Postal postal = response.getPostal();
            Location location = response.getLocation();

            System.out.println("IP地址:" + ip);
            System.out.println("所在国家:" + country.getName());
            System.out.println("所在省份:" + subdivision.getName());
            System.out.println("所在城市:" + city.getName());
            System.out.println("邮政编码:" + postal.getCode());
            System.out.println("经度:" + location.getLongitude());
            System.out.println("纬度:" + location.getLatitude());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

流程图

下面是查询IP归属地的流程图:

st=>start: 开始
op1=>operation: 获取IP地址
cond1=>condition: 是否使用第三方接口查询?
op2=>operation: 查询IP归属地
op3=>operation: 输出结果
e=>end: 结束

st->op1->cond1
cond1(yes)->op2->op3->e
cond1(no)->op2->op3->e

结论

通过以上两种方法,我们可以方便地查询IP地址的归属地信息。根据具体需求选择适合的方法,可以提高应用的性能和用户体验。

希望本文对您理解Java查询IP归属地有所帮助!