admin
2018-12-19 b91b38942de4a865f105c4ca1e7d5ffce1b60b66
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
package org.yeshi.utils;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
 
import net.sf.json.JSONObject;
 
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;
    }
 
    public static Map<String, String> getIPInfo(String ip) {
        Map<String, String> map = new HashMap<String, String>();
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
        try {
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            JSONObject object = JSONObject.fromObject(result);
            if (object.optInt("code") == 0) {
                map.put("city", object.optJSONObject("data").optString("city"));
                map.put("country", object.optJSONObject("data").optString("country"));
                return map;
            }
 
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
 
    public static String getIPContry(String ip) {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
        try {
            client.executeMethod(method);
            String result = method.getResponseBodyAsString();
            JSONObject object = JSONObject.fromObject(result);
            if (object.optInt("code") == 0) {
                return object.optJSONObject("data").optString("country");
            }
 
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
}