当前位置: 代码迷 >> Web前端 >> 获得用户IP地址
  详细解决方案

获得用户IP地址

热度:112   发布时间:2012-08-31 12:55:03.0
取得用户IP地址
public static String getIpAddress(HttpServletRequest request) {
	String ip = request.getHeader("X-Forwarded-For");
	if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(ip, "unknown")) {
		ip = request.getHeader("Proxy-Client-IP");
	}
	if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(ip, "unknown")) {
		ip = request.getHeader("WL-Proxy-Client-IP");
	}
	if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(ip, "unknown")) {
		ip = request.getHeader("HTTP_CLIENT_IP");
	}
	if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(ip, "unknown")) {
		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
	}
	if (StringUtils.isBlank(ip) || StringUtils.equalsIgnoreCase(ip, "unknown")) {
		ip = request.getRemoteAddr();
	}
	if (StringUtils.isNotBlank(ip) && StringUtils.indexOf(ip, ",") > 0) {
		String[] ipArray = StringUtils.split(ip, ",");
		ip = ipArray[0];
	}
	return ip;
}
  相关解决方案