夜_依然的gravatar头像
夜_依然 2014-06-13 15:02:53

java servlet获取客户端请求ip地址

java后台获取当前访问http的IP地址,获取真实的IP,越过各种代理:        

public 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;
        }

这是我同事写的,我就分享一下给需要的朋友!下次可能自己也用到。


最代码官方编辑于2014-8-21 11:18:25


打赏

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友