Administrator
2025-02-13 d248fcaf389ae5c763926955d7ba0cfa80fc002f
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.taoke.autopay.utils;
 
import com.google.gson.Gson;
import lombok.Data;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.net.URLEncoder;
 
public class IPUtil {
 
 
    public static String getRemotIP(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;
    }
 
 
    /**
     * 获取远程端口
     *
     * @param request
     * @return
     */
    public static int getRemotePort(HttpServletRequest request) {
        return request.getRemotePort();
    }
 
    public static IPInfo getLocalIPInfo(String ip) throws Exception {
        String appcode = "0c175db2439b4ef782594b7434187505";
        // 通过阿里云的接口获取IP信息
        String url = "https://zjip.market.alicloudapi.com/lifeservice/QueryIpAddr/query?ip=" + URLEncoder.encode(ip, "UTF-8");
        // 网络请求
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(2000);
        GetMethod method = new GetMethod(url);
        method.setRequestHeader("Authorization",  "APPCODE " + appcode);
        client.executeMethod(method);
        String result = method.getResponseBodyAsString();
        System.out.println(result);
        JSONObject data = JSONObject.fromObject(result);
        if (data.optInt("error_code") == 0) {
            data = data.optJSONObject("result");
            IPInfo ipInfo = new IPInfo(data.optString("country"), data.optString("province"), data.optString("city"));
            return ipInfo;
        } else {
            throw new Exception(data.optString("reason"));
        }
    }
 
    @Data
    public static class IPInfo implements Serializable {
        private String province;
        private String city;
        private String country;
 
        public IPInfo(String country, String province, String city) {
            this.province = province;
            this.city = city;
            this.country = country;
        }
    }
 
    public static void main(String[] args) {
        try {
            IPInfo info = getLocalIPInfo("113.250.254.8");
            System.out.println(new Gson().toJson(info));
            info = getLocalIPInfo("193.112.35.168");
            System.out.println(new Gson().toJson(info));
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}