java中获取用户的真实ip_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java中获取用户的真实ip

java中获取用户的真实ip

 2018/8/21 15:22:38  一边天  程序员俱乐部  我要评论(0)
  • 摘要:获取登录用户的真实ip地址:/***用于获取用户客户端真实的IP地址**@paramrequest*@return*/publicstaticStringgetClentIp(HttpServletRequestrequest){Stringip="";try{ip=request.getHeader("x-forwarded-for");if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request
  • 标签:Java 用户

获取登录用户的真实ip地址:

class="java" name="code">/**
     * 用于获取用户客户端真实的IP地址
     *
     * @param request
     * @return
     */
    public static String getClentIp(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.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
           
        } catch (Exception e) {
            LOG.warn(“客户端IP异常 : " + ExceptionUtil.print(e));
        }
        return ip;
    }

?

发表评论
用户名: 匿名