1. 程式人生 > 其它 >根據IP地址獲取地理位置

根據IP地址獲取地理位置

根據IP地址獲取地理位置

1)根據IP地址獲取地理位置

獲取本機的公網IP,使用介面來查詢對應IP的地理位置。呼叫百度的ip定位api服務,詳情參考:百度地圖api

參考程式碼如下:

package com.example.addressip.utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author relax * @version 1.0 * @功能描述:根據ip獲取地址 * @date 2021/10/11 22:18 */ public class AddressIpUtil { /** * 在使用正則表示式時,利用好其預編譯功能,可以有效加快正則匹配速度。 * 官方提醒:在使用正則表示式時,利用好其預編譯功能,可以有效加快正則匹配速度。 說明:不要在方法體內定義。 * 其實外掛的意思是讓我們把這個資訊抽取到方法外面使其觸發預編譯。 * 通過正則表示式匹配我們想要的內容,根據拉取的網頁內容不同,正則表示式作相應的改變
*/ private static final Pattern pattern = Pattern.compile("顯示IP地址為(.*?)的位置資訊"); private static final String akCode = "A64YQ6I7s7HCqBmXoAQLYdbk2Vd****"; /** * 讀取所有內容 * * @param rd * @return * @throws IOException */ private static String readAll(Reader rd) throws
IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } /** * 拉取網頁所有內容,並轉化為Json格式 * * @param url * @return * @throws IOException * @throws JSONException */ public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); return json; } finally { is.close(); } } public String getAddress() { String ip = ""; String chinaz = "http://ip.chinaz.com"; StringBuilder inputLine = new StringBuilder(); String read = ""; URL url = null; HttpURLConnection urlConnection = null; BufferedReader in = null; try { url = new URL(chinaz); urlConnection = (HttpURLConnection) url.openConnection(); // 如有亂碼的,請修改相應的編碼集,這裡是 gbk in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "gbk")); while ((read = in.readLine()) != null) { inputLine.append(read + "\r\n"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } Matcher m = pattern.matcher(inputLine.toString()); if (m.find()) { String ipStr = m.group(0); // 這裡根據具體情況,來擷取想要的內容 ip = ipStr.substring(ipStr.indexOf("為") + 2, ipStr.indexOf("的") - 1); System.out.println(ip); } JSONObject json = null; String city = null; try { // 這裡呼叫百度的ip定位api服務 詳見 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=" + akCode + "&ip=" + ip); System.out.println(json); city = (((JSONObject) ((JSONObject) json.get("content")) .get("address_detail")).get("city")).toString(); } catch (Exception e) { e.printStackTrace(); } return city; } public static void main(String[] args) { AddressIpUtil Addr = new AddressIpUtil(); String addr = Addr.getAddress(); System.out.println(addr); } }

需要引入的依賴

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

本文來源於CSDN作者:jam_fanatic