JAVA 取得IP地址

/**
	 * 取得IP地址
	 * @param request
	 * @return
	 */
	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.equals("unkown")) {
			ip = request.getHeader("Proxy-Client-IP");
				if (ip == null || ip.equals("unkown")) {
						ip = request.getHeader("WL-Proxy-Client-IP");
						if (ip == null || ip.equals("unkown")) {
							ip = request.getRemoteAddr();
						}
				}
		 }

		String ipaddr = "";
		if (ip.indexOf(",") > 0) {
			ipaddr = ip.substring(0, ip.indexOf(","));
		} else {
			ipaddr = ip;
		}
		return ipaddr;
	}

具体不解释 

中间遇到了一个问题,就是取得的IP地址永远都是192.168.3.6。 后来才发现原来测试环境的网络是经过网闸控制的,放到生产环境服务器就没问题了。阿弥托福

猜你喜欢

转载自yiqila.iteye.com/blog/1129466
今日推荐