怎样获取用户的真实ip求代码,

Crazy_427 2015-09-11 04:26:13
获得客户端真实ip地址的体例一:
  public String getRemortIP(HttpServletRequest request) {
  if (request.getHeader("x-forwarded-for") == null) {
  return request.getRemoteAddr();
  }
  return request.getHeader("x-forwarded-for");
  }
  获得客户端真实ip地址的体例二:
  public 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.getRemoteAddr();
  }
  return ip;
  }
这是获取的结果是0:0:0:0:0:0:0:1不是我想要的结果
...全文
50 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
DaveLeeCN 2015-09-11
你说的是外网Ip吧,这个貌似本机还真没有什么办法,通常是你联网之后人家才能看到你的ip
回复
Braska 2015-09-11
if ("0:0:0:0:0:0:0:1".equals(ip)) {
		ip = InetAddress.getLocalHost().getHostAddress();
	}
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-11 04:26
社区公告
暂无公告