java代码获取客户端的真实ip

protected 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();    
        }    
        System.out.println("客户端ip为 :"+ip);
        return ip;    
}

 

posted on 2016-02-25 10:29  WesTward  阅读(324)  评论(0编辑  收藏  举报