java获取本地IP和服务器IP

java获取IP地址

  • 获取本地IP地址
    //这种方法能不能在Linux服务器上直接获取IP有待验证
    String address = InetAddress.getLocalHost().getHostAddress().toString();
  • 获取服务器IP地址(针对nginx)
    package com.utils;
     
    import javax.servlet.http.HttpServletRequest;
     
    public class GetIP {
    	/**
    	*Auther:呐喊
    	*Function:获取客户端IP地址,针对Nginx等反代作处理
    	*Date:2016-11-01
    	*URL:http://www.htcdc.com
    	*/
    	public String getIP(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.getHeader("X-Real-IP");
    		}
    		if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
    			ip=request.getRemoteAddr();
    		}
    		return ip;
    	}
    	
    	
    }
    

你可能感兴趣的:(java获取本地IP和服务器IP)