Home 获取客户端的IP地址
Post
Cancel

获取客户端的IP地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public String getIpAddr(HttpServletRequest request) {
    String ip = "";
    try {
        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();
        }
    } catch (Exception e) {
        e.printStackTrace();
        ip = "";
    }
    return ip;
}
This post is licensed under CC BY 4.0 by the author.