本文实例讲述了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
百度地图开放平台:
/**
* 读取
*
* @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();
}
}
根据
/**
* 百度获取城市信息
*
* @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);
}
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);
}
}