无关风月
2025-02-14 11ec1c23a9f47ed70b124e413e33e3696897390f
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.jilongda.common.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.jilongda.common.exception.ServiceException;
 
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
 
/**
 * @author xiaochen
 * @version 1.0
 * @ClassName: IPUtil
 * @Desc: Ip工具类
 * @date 2021/12/21
 * @history v1.0
 */
public class IPUtil {
 
    /**
     * 描述:获取IP地址
     *
     * @param request
     * @return
     * @author xiaochen
     * @date 2021/12/21
     */
    public static String getIpAddress(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();
        }
        // 如果是多级代理,那么取第一个IP为客户端IP
        if (ip != null && ip.indexOf(",") != -1) {
            ip = ip.substring(0, ip.indexOf(",")).trim();
        }
        return ip;
    }
 
 
    /**
     * 描述:获取IP+[IP所属地址]
     *
     * @param request
     * @return
     * @author huaping hu
     * @date 2016年6月1日下午6:01:09
     */
    public static String getIpBelongAddress(HttpServletRequest request) {
 
        String ip = getIpAddress(request);
        String belongIp = getIPbelongAddress(ip);
 
        return ip + belongIp;
    }
 
    /**
     * 描述:获取IP所属地址
     *
     * @param ip
     * @return
     * @author huaping hu
     * @date 2016年6月1日下午5:59:43
     */
    public static String getIPbelongAddress(String ip) {
 
        String ipAddress = "[]";
        try {
            //淘宝提供的服务地址
            String context = call("http://ip.taobao.com?ip=" + ip);
            JSONObject fromObject = JSONObject.parseObject(context);
            String code = fromObject.getString("code");
            if (code.equals("0")) {
                JSONObject jsonObject = fromObject.getJSONObject("data");
                ipAddress = "[" + jsonObject.get("country") + "/" + jsonObject.get("city") + "]";
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取IP所属地址出错", e);
        }
        return ipAddress;
    }
 
    /**
     * 描述:获取Ip所属地址
     *
     * @param urlStr
     * @return
     * @author huaping hu
     * @date 2016年6月1日下午5:38:55
     */
    public static String call(String urlStr) {
 
        try {
 
            URL url = new URL(urlStr);
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
 
            httpCon.setConnectTimeout(3000);
            httpCon.setDoInput(true);
            httpCon.setRequestMethod("GET");
 
            int code = httpCon.getResponseCode();
 
            if (code == 200) {
                return streamConvertToSting(httpCon.getInputStream());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取IP所属地址出错", e);
        }
        return null;
    }
 
    /**
     * 描述:将InputStream转换成String
     *
     * @param is
     * @return
     * @author huaping hu
     * @date 2016年6月1日下午5:51:53
     */
    public static String streamConvertToSting(InputStream is) {
 
        String tempStr = "";
        try {
 
            if (is == null) return null;
            ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
            byte[] by = new byte[1024];
            int len = 0;
            while ((len = is.read(by)) != -1) {
                arrayOut.write(by, 0, len);
            }
            tempStr = new String(arrayOut.toByteArray());
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tempStr;
    }
 
    /**
     * 获取用户ip地址
     *
     * @return
     */
    public static String getUserIp() {
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        //获取IP地址
        return getIpAddress(request);
    }
 
    public static String getUserLocationByIp() {
        String url = "https://restapi.amap.com/v3/ip";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("key", "5935f1310fa27e918e99c620f2afb39f");
        paramMap.put("ip", getUserIp());
        String result1 = HttpUtils.getReq(url, paramMap);
        JSONObject jsonObject = JSONObject.parseObject(result1);
        IpLocation ipLocation = jsonObject.toJavaObject(IpLocation.class);
        return ipLocation.getProvince() + ipLocation.getCity();
    }
 
 
    public static void main(String[] args) {
 
        String context = call("http://ip.taobao.com/service/getIpInfo.php?ip=120.192.182.1");
 
        JSONObject fromObject = JSONObject.parseObject(context);
        //JSONObject jsonObject = fromObject.getJSONObject("data");
        System.out.println(fromObject);
        //System.err.println(jsonObject.get("city"));
    }
}