当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

根据HttpServletRequest取得ip

发表于: 2013-01-21   作者:dreamoftch   来源:转载   浏览次数:
IP
摘要: public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCas
public static 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.getHeader("Cdn-Src-Ip");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}


如果是要获得本机的IP的话可以通过:

InetAddress.getLocalHost().toString()获得

 

 

public String getClientIP(HttpServletRequest request){
		String ip = request.getHeader("x-real-ip");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-forwarded-for");
			if(ip != null) {
				ip=ip.split(",")[0].trim();
			}
		}
		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;       
	}

 

根据HttpServletRequest取得ip

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
如果WordPress运行在Nginx作为proxy代理的后端,那么,WP会默认取得$_SERVER['remote_addr']的IP地址.
如果WordPress运行在Nginx作为proxy代理的后端,那么,WP会默认取得$_SERVER['remote_addr']的IP地址.
1、根据IP地址获取机器名,Ping 的命令用法: ping /a 192.168.1.1 2、根据 IP 获得MAC地址 nbtstat
其实代码是网上找的,自己做了一点小的改动 前期的准备就是下载qqwry.dat这个文件,下载地址请参考
根据访问者的ip地址获得当地的天气情况,最简单的一种方法就是加一个iframe框架方法如下: <ifra
1.核心:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=192.152.3.25 把这句
1.下载ip数据库 GeoIP.dat.gz GeoLiteCity.dat.gz wget http://geolite.maxmind.com/download/geoip
有时因为需要,希望知道域名的IP,那用C#怎么实现呢?以下是实现代码 using System; using System.C
大家应该都知道Ping是做什么的吧,如果不知道的话,你单击开始---运行---输入 ping www.baidu.com -
阅读全文下载例子:http://www.sufeinet.com/forum.php?mod=viewthread&tid=402 大家应该都知道Ping
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号