获取当前电脑的ip地址

String client_ip = request.getHeader("x-forwarded-for");
if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) {
    client_ip = request.getHeader("Proxy-Client-IP");
}
if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) {
    client_ip = request.getHeader("WL-Proxy-Client-IP");
}
if(client_ip == null || client_ip.length() == 0 || "unknown".equalsIgnoreCase(client_ip)) {
    client_ip = request.getRemoteAddr();
}  
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割  
if(client_ip != null && client_ip.length() > 15){ //"***.***.***.***".length() = 15  
    if(client_ip.indexOf(",") > 0){
        client_ip = client_ip.substring(0,client_ip.indexOf(","));
    }
}

 

posted @ 2018-01-26 15:22  aLa神灯  阅读(978)  评论(0编辑  收藏  举报