读书人

java 取得客户端真是ip地址

发布时间: 2012-08-25 10:06:20 作者: rapoo

java 获得客户端真是ip地址

?

 /**     * ip校验     * @param s     * @return 格式是否正确     */    public Boolean isIpAddress(String s){         String regex = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";         Pattern p = Pattern.compile(regex);         Matcher m = p.matcher(s);         return m.matches();    }    /**     * 获取客户端ip     * @param request     * @return ip 地址     */    public String getClientAddress(HttpServletRequest request) {        String address = request.getHeader("X-Forwarded-For");        if (address != null && isIpAddress(address)) {            return address;        }        return request.getRemoteAddr();    }

读书人网 >软件架构设计

热点推荐