首页 > 代码库 > 获取客户端真实IP地址

获取客户端真实IP地址

1、需要引入log4j-1.2.14.jar

package org.ydd.test;


import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 * @author coco
 * @version 
 * 获取IP4
 */
public class IP4 {
	private static final Logger log = Logger.getLogger(IP4.class);
	/**
	 * 获取客户端真实IP地址(注意:如果通过Squid反向代理软件,需更改squid.conf配置文件forwarded_for项默认是为on,
	 * 否则获取到的IP为unknown)
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  static String getIpAddr(HttpServletRequest request) {
	log.debug("====================================================");

	if(log.isDebugEnabled()){
	StringBuffer buf = new StringBuffer("all head info:\n");
	Enumeration enumeration = request.getHeaderNames();
	while(enumeration.hasMoreElements()){
	Object head = enumeration.nextElement();
	if(null != head){
	String value = http://www.mamicode.com/request.getHeader(String.valueOf(head));>

获取客户端真实IP地址