cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.core.util;
 
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang.StringUtils;
 
import cn.ksource.core.CascadeMap;
 
public class IPUtil {
 
    public static String getIpAddr(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.getRemoteAddr();
        }
        return ip;
    }
    
    /**
     * 获取本机IP地址
     * @return
     * @version V1.0.0
     * @author 杨凯
     * @throws HttpRemoteException 
     * @date Jan 25, 2014 2:21:05 PM
     */
    public static IPEntity getIpEntity() throws HttpRemoteException{
        IPEntity ipEntity = new IPEntity();
        ipEntity.setLanIP(getLocalHostIp());
        String content = HttpUtil.doGet("http://pv.sohu.com/cityjson", null, HttpCharset.GBK);
        content = StringUtils.removeStart(content, "var returnCitySN = ");
        content = StringUtils.removeEnd(content, ";");
        Map ipMap = JsonUtil.json2Map(content);
        ipEntity.setInternetIP(ConvertUtil.obj2Str(ipMap.get("cip")));
        ipEntity.setPosition(ConvertUtil.obj2Str(ipMap.get("cname")));
        return ipEntity;
    }
    
    public static void main(String[] args) throws HttpRemoteException {
        String content = HttpUtil.doGet("http://pv.sohu.com/cityjson", null, HttpCharset.GBK);
        content = StringUtils.removeStart(content, "var returnCitySN = ");
        content = StringUtils.removeEnd(content, ";");
        
        Map ipMap = JsonUtil.json2Map(content);
        System.out.println(ipMap.get("cid"));
        
        String ip = content.replaceAll(":",":");
        
        System.out.println(content);
        System.out.println(ip);
    }
    
    
    /**
     * 获取本机IP地址,操作系统为Windows,Linux上返回127.0.0.1
     * 杨凯
     * @return
     */
    public static String getLocalHostIp(){
        String ip ="";
        
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }
 
    /**
     * 是否内网IP
     * @param ipAddress
     * @return
     * @version V1.0.0
     * @author 杨凯
     * @date Jan 25, 2014 1:29:57 PM
     */
    public static boolean isInnerIP(String ipAddress) {
        
        if (ipAddress.equals("127.0.0.1")) {
            return true;
        }
        
        long ipNum = getIpNum(ipAddress);
        
        /**
         * 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类
         * 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
         */
        List<Map<String, String>> ipList = new ArrayList<Map<String,String>>();
        ipList.add(new CascadeMap().put("START_IP", "10.0.0.0").put("END_IP", "10.255.255.255"));
        ipList.add(new CascadeMap().put("START_IP", "172.16.0.0").put("END_IP", "172.31.255.255"));
        ipList.add(new CascadeMap().put("START_IP", "192.168.0.0").put("END_IP", "192.168.255.255"));
        ipList.add(new CascadeMap().put("START_IP", "1.1.0.0").put("END_IP", "1.1.255.255"));
        
        
        for (Map<String, String> map : ipList) {
            if ((ipNum >= getIpNum(map.get("START_IP")) && ipNum <= getIpNum(map.get("END_IP")))) {
                return true;
            }
        }
        return false;
    }
 
    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[2]);
        long d = Integer.parseInt(ip[3]);
 
        long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
        return ipNum;
    }
 
 
}