码迷,mamicode.com
首页 > 其他好文 > 详细

获取用户ip地址,为什么获取不到代理过之后的

时间:2014-12-14 07:04:50      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   java   

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="org.apache.commons.lang3.StringUtils;"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<%!
    public static String getRmoteIP(HttpServletRequest request){
    String ip = request.getHeader("X-Forwarded-For");
    if(StringUtils.isNotBlank(ip)){
        String[] ips = StringUtils.split(ip, ",");
        if(ips != null){
            for(String tmpIp: ips){
                if(StringUtils.isBlank(tmpIp))
                    continue;
                tmpIp = tmpIp.trim();
                if(tmpIp != null && !tmpIp.startsWith("10.") && !tmpIp.startsWith("192.")){
                    return tmpIp.trim();
                }
            }
        }
    }
    ip = request.getHeader("x-real-ip");
    if(isIPAddr(ip))
        return ip;
    ip = request.getRemoteAddr();
    if(ip.indexOf(".") != -1)
        ip = "127.0.0.1";
    return ip;
}
 
 
public static boolean isIPAddr(String addr){
    if(StringUtils.isBlank(addr))
        return false;
    String[] ips = StringUtils.split(addr,".");
    if(ips.length != 4)
        return false;
    try{
        int ipa = Integer.parseInt(ips[0]);
        int ipb = Integer.parseInt(ips[1]);
        int ipc = Integer.parseInt(ips[2]);
        int ipd = Integer.parseInt(ips[3]);
     
        return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0 && ipc <= 255
                && ipd >= 0 && ipd <= 255;
    }catch(Exception e){}
    return false;
}
 
public static String getDomainOfServerName(String host){
    if(isIPAddr(host))
        return null;
     
    String[] names = StringUtils.split(host, ".");
    int len = names.length;
    if(len == 1) return null;
    if(len == 3)
        return makeup(names[len -2], names[len - 1]);
    if(len > 3){
        String dp = names[len - 2];
        if(dp.equalsIgnoreCase("com") || dp.equalsIgnoreCase("gov") || dp.equalsIgnoreCase("cn"))
            return makeup(names[len - 3], names[len - 2], names[len - 1]);
        else
            return makeup(names[len -2], names[len - 1]);
    }
    return host;
}
/*
 * 拼接
http://club.jd.com/CommunityGroupDetail/13505169_1.html
http://club.jd.com/CommunityGroupDetail/13505249_1.html
http://club.jd.com/CommunityGroupDetail/13505503_1.html
http://club.jd.com/CommunityGroupDetail/13505427_1.html
http://club.jd.com/CommunityGroupDetail/13505429_1.html
http://club.jd.com/CommunityGroupDetail/13504990_1.html
http://club.jd.com/CommunityGroupDetail/13505390_1.html
http://club.jd.com/CommunityGroupDetail/13505372_1.html
http://club.jd.com/CommunityGroupDetail/13505633_1.html
http://club.jd.com/CommunityGroupDetail/13505969_1.html
http://club.jd.com/CommunityGroupDetail/13506145_1.html
http://club.jd.com/CommunityGroupDetail/13506401_1.html
http://club.jd.com/CommunityGroupDetail/13506414_1.html
http://club.jd.com/CommunityGroupDetail/13506334_1.html
http://club.jd.com/CommunityGroupDetail/13506777_1.html
http://club.jd.com/CommunityGroupDetail/13506892_1.html
http://club.jd.com/CommunityGroupDetail/13507094_1.html
http://club.jd.com/CommunityGroupDetail/13506978_1.html
http://club.jd.com/CommunityGroupDetail/13506808_1.html
http://club.jd.com/CommunityGroupDetail/13507159_1.html
http://club.jd.com/CommunityGroupDetail/13507049_1.html
http://club.jd.com/CommunityGroupDetail/13507019_1.html
http://club.jd.com/CommunityGroupDetail/13506909_1.html
http://club.jd.com/CommunityGroupDetail/13507271_1.html
http://club.jd.com/CommunityGroupDetail/13507242_1.html
http://club.jd.com/CommunityGroupDetail/13507164_1.html
http://club.jd.com/CommunityGroupDetail/13506878_1.html
http://club.jd.com/CommunityGroupDetail/13507243_1.html
http://club.jd.com/CommunityGroupDetail/13507146_1.html
http://club.jd.com/CommunityGroupDetail/13507143_1.html
http://club.jd.com/CommunityGroupDetail/13507245_1.html
http://club.jd.com/CommunityGroupDetail/13507244_1.html
http://club.jd.com/CommunityGroupDetail/13507167_1.html
http://club.jd.com/CommunityGroupDetail/13507198_1.html
http://club.jd.com/CommunityGroupDetail/13507662_1.html
http://club.jd.com/CommunityGroupDetail/13507322_1.html
http://club.jd.com/CommunityGroupDetail/13507289_1.html
http://club.jd.com/CommunityGroupDetail/13507293_1.html
http://club.jd.com/CommunityGroupDetail/13507445_1.html
http://club.jd.com/CommunityGroupDetail/13507611_1.html
http://club.jd.com/CommunityGroupDetail/13507293_1.html
http://club.jd.com/CommunityGroupDetail/13507616_1.html
http://club.jd.com/CommunityGroupDetail/13507698_1.html
http://club.jd.com/CommunityGroupDetail/13507616_1.html
http://club.jd.com/CommunityGroupDetail/13507358_1.html
http://club.jd.com/CommunityGroupDetail/13507752_1.html
http://club.jd.com/CommunityGroupDetail/13507655_1.html
http://club.jd.com/CommunityGroupDetail/13507731_1.html
http://club.jd.com/CommunityGroupDetail/13507360_1.html
http://club.jd.com/CommunityGroupDetail/13507813_1.html
http://club.jd.com/CommunityGroupDetail/13507991_1.html
http://club.jd.com/CommunityGroupDetail/13508051_1.html
http://club.jd.com/CommunityGroupDetail/13507741_1.html
http://club.jd.com/CommunityGroupDetail/13508051_1.html
http://club.jd.com/CommunityGroupDetail/13507845_1.html
http://club.jd.com/CommunityGroupDetail/13507742_1.html
http://club.jd.com/CommunityGroupDetail/13507741_1.html
http://club.jd.com/CommunityGroupDetail/13507995_1.html
http://club.jd.com/CommunityGroupDetail/13507969_1.html
http://club.jd.com/CommunityGroupDetail/13507996_1.html
http://club.jd.com/CommunityGroupDetail/13507686_1.html
http://club.jd.com/CommunityGroupDetail/13508056_1.html
http://club.jd.com/CommunityGroupDetail/13507825_1.html
http://club.jd.com/CommunityGroupDetail/13507977_1.html
http://club.jd.com/CommunityGroupDetail/13507979_1.html
http://club.jd.com/CommunityGroupDetail/13507858_1.html
http://club.jd.com/CommunityGroupDetail/13508095_1.html
http://club.jd.com/CommunityGroupDetail/13508030_1.html
http://club.jd.com/CommunityGroupDetail/13508119_1.html
http://club.jd.com/CommunityGroupDetail/13507803_1.html
http://club.jd.com/CommunityGroupDetail/13508036_1.html
http://club.jd.com/CommunityGroupDetail/13508205_1.html
http://club.jd.com/CommunityGroupDetail/13508238_1.html
http://club.jd.com/CommunityGroupDetail/13507957_1.html
http://club.jd.com/CommunityGroupDetail/13508188_1.html
http://club.jd.com/CommunityGroupDetail/13508189_1.html
http://club.jd.com/CommunityGroupDetail/13508352_1.html
http://club.jd.com/CommunityGroupDetail/13508241_1.html
http://club.jd.com/CommunityGroupDetail/13508352_1.html
http://club.jd.com/CommunityGroupDetail/13508471_1.html
http://club.jd.com/CommunityGroupDetail/13508045_1.html
http://club.jd.com/CommunityGroupDetail/13508042_1.html
http://club.jd.com/CommunityGroupDetail/13508387_1.html
http://club.jd.com/CommunityGroupDetail/13508447_1.html
http://club.jd.com/CommunityGroupDetail/13508389_1.html
http://club.jd.com/CommunityGroupDetail/13508248_1.html
http://club.jd.com/CommunityGroupDetail/13508474_1.html
http://club.jd.com/CommunityGroupDetail/13508474_1.html
http://club.jd.com/CommunityGroupDetail/13508248_1.html
http://club.jd.com/CommunityGroupDetail/13508476_1.html
http://club.jd.com/CommunityGroupDetail/13508255_1.html
http://club.jd.com/CommunityGroupDetail/13508425_1.html
http://club.jd.com/CommunityGroupDetail/13508401_1.html
http://club.jd.com/CommunityGroupDetail/13508454_1.html
http://club.jd.com/CommunityGroupDetail/13508425_1.html
http://club.jd.com/CommunityGroupDetail/13508575_1.html
http://club.jd.com/CommunityGroupDetail/13508362_1.html
http://club.jd.com/CommunityGroupDetail/13508863_1.html
http://club.jd.com/CommunityGroupDetail/13508586_1.html
http://club.jd.com/CommunityGroupDetail/13508698_1.html
http://club.jd.com/CommunityGroupDetail/13508586_1.html
http://club.jd.com/CommunityGroupDetail/13508698_1.html
http://club.jd.com/CommunityGroupDetail/13509463_1.html
http://club.jd.com/CommunityGroupDetail/13509037_1.html
http://club.jd.com/CommunityGroupDetail/13509465_1.html
http://club.jd.com/CommunityGroupDetail/13509444_1.html
http://club.jd.com/CommunityGroupDetail/13509503_1.html
http://club.jd.com/CommunityGroupDetail/13509500_1.html
http://club.jd.com/CommunityGroupDetail/13509217_1.html
http://club.jd.com/CommunityGroupDetail/13510459_1.html
http://club.jd.com/CommunityGroupDetail/13510429_1.html
http://club.jd.com/CommunityGroupDetail/13510351_1.html
http://club.jd.com/CommunityGroupDetail/13510167_1.html
http://club.jd.com/CommunityGroupDetail/13510503_1.html
http://club.jd.com/CommunityGroupDetail/13510504_1.html
http://club.jd.com/CommunityGroupDetail/13510295_1.html
http://club.jd.com/CommunityGroupDetail/13510236_1.html
http://club.jd.com/CommunityGroupDetail/13510719_1.html
http://club.jd.com/CommunityGroupDetail/13511579_1.html
http://club.jd.com/CommunityGroupDetail/13511787_1.html
http://club.jd.com/CommunityGroupDetail/13512161_1.html
http://club.jd.com/CommunityGroupDetail/13512191_1.html
http://club.jd.com/CommunityGroupDetail/13512350_1.html
http://club.jd.com/CommunityGroupDetail/13512158_1.html
http://club.jd.com/CommunityGroupDetail/13512293_1.html
http://club.jd.com/CommunityGroupDetail/13512385_1.html
http://club.jd.com/CommunityGroupDetail/13512356_1.html
http://club.jd.com/CommunityGroupDetail/13512445_1.html
http://club.jd.com/CommunityGroupDetail/13512391_1.html
http://club.jd.com/CommunityGroupDetail/13512241_1.html
http://club.jd.com/CommunityGroupDetail/13512190_1.html
http://club.jd.com/CommunityGroupDetail/13512527_1.html
http://club.jd.com/CommunityGroupDetail/13512646_1.html
http://club.jd.com/CommunityGroupDetail/13512476_1.html
http://club.jd.com/CommunityGroupDetail/13512249_1.html
http://club.jd.com/CommunityGroupDetail/13512562_1.html
http://club.jd.com/CommunityGroupDetail/13512701_1.html
http://club.jd.com/CommunityGroupDetail/13512823_1.html
http://club.jd.com/CommunityGroupDetail/13512794_1.html
http://club.jd.com/CommunityGroupDetail/13512483_1.html
http://club.jd.com/CommunityGroupDetail/13512825_1.html
http://club.jd.com/CommunityGroupDetail/13512826_1.html
http://club.jd.com/CommunityGroupDetail/13512828_1.html
http://club.jd.com/CommunityGroupDetail/13512628_1.html
http://club.jd.com/CommunityGroupDetail/13512629_1.html
http://club.jd.com/CommunityGroupDetail/13512658_1.html

获取用户ip地址,为什么获取不到代理过之后的

标签:style   http   io   ar   color   os   sp   for   java   

原文地址:http://blog.csdn.net/leed2012/article/details/41921543

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!