zoukankan      html  css  js  c++  java
  • 获取客户端IP地址

    /**
      * 获取客户端IP,可以经过一级代理
      * @modify by lingl 2012-06-14
      * @param pd
      * @return
      * @throws Exception
      */
     public static String getIPAddress(PageData pd) throws Exception {
      HttpServletRequest request = pd.getRequest();
      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();
      }
      return ip.split(",")[0];
     }
     
     /**
      * 获取客户端IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
      * getClientIP
      *
      * @param pd
      * @return
      * @throws Exception String
      * @Exception
      * @author:lingl@asiainfo-linkage.com
      * @2012-7-20 上午11:06:14
      * @update:
      * @2012-7-20 上午11:06:14
      */
     public static String getClientIP(PageData pd) throws Exception  {
      HttpServletRequest request = pd.getRequest();
      String strClientIp = request.getHeader("x-forwarded-for");
      log.debug("=======All the IP address string is: " + strClientIp);
      if(strClientIp == null || strClientIp.length() == 0 ||"unknown".equalsIgnoreCase(strClientIp)){ 
       strClientIp = request.getRemoteAddr();
      }else{
       String[] ipList  = strClientIp.split(",");
       String strIp = new String();
       for(int index = 0; index < ipList.length; index ++){
        strIp = (String)ipList[index];
        if(!("unknown".equalsIgnoreCase(strIp))){
         strClientIp = strIp;
         break;
        }
       }
      }
      return strClientIp;
     }

    /**
      * 获取客户端IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
      * getClientIP
      *
      * @param pd
      * @return
      * @throws Exception String
      * @Exception
      * @author:lingl@asiainfo-linkage.com
      * @2012-7-20 上午11:06:14
      * @update:
      * @2012-7-20 上午11:06:14
      */
     public static String getClientIP(PageData pd) throws Exception  {
      HttpServletRequest request = pd.getRequest();
      String strClientIp = request.getHeader("x-forwarded-for");
      log.debug("=======All the IP address string is: " + strClientIp);
      if(strClientIp == null || strClientIp.length() == 0 ||"unknown".equalsIgnoreCase(strClientIp)){ 
       strClientIp = request.getRemoteAddr();
      }else{
       String[] ipList  = strClientIp.split(",");
       String strIp = new String();
       for(int index = 0; index < ipList.length; index ++){
        strIp = (String)ipList[index];
        if(!("unknown".equalsIgnoreCase(strIp))){
         strClientIp = strIp;
         break;
        }
       }
      }
      return strClientIp;
     }

  • 相关阅读:
    数据库学习笔记3--基本的SQL语句
    数据库学习笔记2--MySQL数据类型
    数据库学习笔记1----MySQL 5.6.21的安装和配置(setup版)
    JavaWeb学习笔记1---http协议
    Spring学习笔记18--通过工厂方法配置Bean
    Spring学习笔记17--在XML中使用SPEL
    Spring 学习笔记16---properties文件的两种方式
    Spring学习笔记15--注解Bean
    Spring4.0学习笔记1---开发环境搭建
    Installed JREs时 Standard 1.1.x VM与Standard VM的区别
  • 原文地址:https://www.cnblogs.com/bassd/p/2848791.html
Copyright © 2011-2022 走看看