java
主页 > 软件编程 > java >

Java获取用户访问IP及地理位置的方法

2020-04-11 | 秩名 | 点击:

获取用户访问的IP地址

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 获取用户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/

创建连接,并读取返回的json数据,返回一个json格式的数据。

对json转换不了解的可以访问:Alibaba Fastjson——超好用的JOSN解析库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * 读取
 *
 * @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格式数据:

获取请求返回的数据(根据自己需求去获取)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 百度获取城市信息
 *
 * @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);
}
 

输出

调用网上的根据ip获取定位的请求

根据http://freeapi.ipip.net/ip这个网址可以获取到ip对应的地理位置,之后发送请求去解析json数据

和上面方法基本一样,上面以为是一个json对象格式,这里是一个json数组格式,所以转换和获取数据方法不太一样具体代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
    }
}
 

输出结果

原文链接:https://blog.csdn.net/qq_40205116/article/details/102883086
相关文章
最新更新