ipアドレスツールクラス


package com.common.utils.httpclient;


import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import javax.servlet.http.HttpServletRequest;

/**
 * @author  nachuan
 * ip     
 */
public class IpUtils {

	/**
	 *     ip   
	 * @param req
	 * @return
	 */
	public static String getContextPath(HttpServletRequest req){
		return req.getScheme()+"://"+ req.getServerName() +":"+req.getServerPort()+req.getContextPath();
	}
	
	public static String  getServerName(HttpServletRequest req) {
		return req.getScheme()+"://"+ req.getServerName()+":"+req.getServerPort();
	}

	public  static  String getProjectRealPath (HttpServletRequest req){
		return  req.getSession().getServletContext().getRealPath("");
	}
	/**
	 *       IP  
	 * 
	 * @param request
	 * @return
	 */
	public static 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();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "  ";
		}
		return ip;
	}
	/**
	 *   webservice  ip
	 * @return
	 */
	public static String getClientIp(){  
	    Message message = PhaseInterceptorChain.getCurrentMessage();  
	    HttpServletRequest httprequest = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);  
	    return httprequest.getRemoteAddr(); 
	}

}