获取远程主机IP工具类

/**
 * 网络工具类
 */
public class NetHelper {
	/**
	 * 获取远程主机IP
	 * @param request
	 * @return
	 */
	public static String getRemoteAddress(HttpServletRequest request) {
		String forwards = request.getHeader("x-forwarded-for");
		if (forwards == null || forwards.trim().length() == 0 || "unknown".equalsIgnoreCase(forwards)) {
			forwards = request.getHeader("Proxy-Client-IP");
		}
		if (forwards == null || forwards.trim().length() == 0 || "unknown".equalsIgnoreCase(forwards)) {
			forwards = request.getHeader("WL-Proxy-Client-IP");
		}
		if (forwards == null || forwards.trim().length() == 0 || "unknown".equalsIgnoreCase(forwards)) {
			forwards = request.getRemoteAddr();
		}
		if (forwards == null || forwards.trim().length() == 0 || "unknown".equalsIgnoreCase(forwards)) {
			forwards = request.getHeader("X-Real-IP");
		}
		if (forwards != null && forwards.trim().length() > 0) {
			String[] ips = forwards.split(",");
			return ips[0];
		}
		return forwards;
	}
}

 

你可能感兴趣的:(java,IP地址,微风,java获取ip,获取远程主机ip)