1. 程式人生 > 程式設計 >Java獲取使用者訪問IP及地理位置的方法詳解

Java獲取使用者訪問IP及地理位置的方法詳解

本文例項講述了Java獲取使用者訪問IP及地理位置的方法。分享給大家供大家參考,具體如下:

獲取使用者訪問的IP地址

/**
 * 獲取使用者ip地址
 * @return
 */
public static String getIp(HttpServletRequest request){
  String ip = request.getHeader("x-forwarded-for");
  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("Proxy-Client-IP");
  }
  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("WL-Proxy-Client-IP");
  }
  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("HTTP_CLIENT_IP");
  }
  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  }
  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getRemoteAddr();
  }
  return ip;
}

IP地址獲取到後可以根據ip地址獲取地址位置

獲取ip地址有多種方法,可以呼叫百度,高度地圖的ip定位api服務,也可以呼叫網上的根據ip獲取定位的請求

高度地圖的ip定位api服務獲取

呼叫百度的ip定位api服務 詳見​​​http://lbsyun.baidu.com/index.php?title=webapi/ip-api

首先需要在百度地圖開放平臺申請一個百度地圖的ak

百度地圖開放平臺:http://lbsyun.baidu.com/

Java獲取使用者訪問IP及地理位置的方法詳解

建立連線,並讀取返回的json資料,返回一個json格式的資料。

對json轉換不瞭解的可以訪問:Alibaba Fastjson——超好用的JOSN解析庫

/**
 * 讀取
 * 
 * @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();
}
 
/**
 * 建立連結
 * 
 * @param url
 * @return
 * @throws IOException
 * @throws JSONException
 */
private 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 = JSONObject.parseObject(jsonText);
		return json;
	} finally {
		is.close();
	}
}

根據http://api.map.baidu.com/location/ip?ip="+ip+"&ak="+ak這個網址去請求地理位置的json資料

返回的json格式資料:

Java獲取使用者訪問IP及地理位置的方法詳解

獲取請求返回的資料(根據自己需求去獲取)

/**
 * 百度獲取城市資訊
 * 
 * @param ip
 * @return
 * @throws JSONException
 * @throws IOException
 */
public static void main(String[] args) throws JSONException,IOException {
	String ip = "";
	// 百度地圖申請的ak
	String ak = "";
	// 這裡呼叫百度的ip定位api服務 詳見 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm
	JSONObject json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ip="+ip+"&ak="+ak);
	
  //這裡只取出了兩個引數,根據自己需求去獲取
  JSONObject obj = (JSONObject) ((JSONObject) json.get("content")).get("address_detail");
	String province = obj.getString("province");
	System.out.println(province);
	
	JSONObject obj2 = (JSONObject) json.get("content");
	String address = obj2.getString("address");
	System.out.println(address);
}

輸出

Java獲取使用者訪問IP及地理位置的方法詳解

呼叫網上的根據ip獲取定位的請求

根據http://freeapi.ipip.net/ip這個網址可以獲取到ip對應的地理位置,之後傳送請求去解析json資料

Java獲取使用者訪問IP及地理位置的方法詳解

和上面方法基本一樣,上面以為是一個json物件格式,這裡是一個json陣列格式,所以轉換和獲取資料方法不太一樣具體程式碼如下

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
 
public class pc1 {
	/**
	 * 讀取
	 * 
	 * @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();
	}
	
	/**
	 * 建立連結
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 * @throws JSONException
	 */
	private static JSONArray 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);
			JSONArray json = JSONArray.parseArray(jsonText);
			return json;
		} finally {
			is.close();
		}
	}
 
	/**
	 * 獲取城市資訊
	 * 
	 * @param ip
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public static void main(String[] args) throws JSONException,IOException {
		String ip = "122.189.200.141";
		JSONArray json = readJsonFromUrl("http://freeapi.ipip.net/"+ip);
		String a1 = (String)json.get(0);
		String a2 = (String)json.get(1);
		String a3 = (String)json.get(2);
		String a4 = (String)json.get(3);
		String a5 = (String)json.get(4);
		System.out.println(a1);
		System.out.println(a2);
		System.out.println(a3);
		System.out.println(a4);
		System.out.println(a5);
	}
}

輸出結果

Java獲取使用者訪問IP及地理位置的方法詳解

更多關於java相關內容感興趣的讀者可檢視本站專題:《Java網路程式設計技巧總結》、《Java Socket程式設計技巧總結》、《Java檔案與目錄操作技巧彙總》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》和《Java快取操作技巧彙總》

希望本文所述對大家java程式設計有所幫助。