服务器IP获取工具类 package com.tartary.turbo.core.util;import javax.servlet.http.HttpServletRequest;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.UnknownHostException;im
package com.tartary.turbo.core.util;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
/**
* @Description 服务器IP获取工具类
* @ClassName ServerIpUtil
* @author yewenting2013@foxmail.com
* @date 2017年8月8日 下午5:56:44
*/
public class ServerIpUtil {
private static final String DEFAULT_SERVER_IP = "127.0.0.1";
/**
* @Description 获取本机IP
* @Title getLocalIp
* @return
*/
public static String getLocalIp() {
String localIp = DEFAULT_SERVER_IP;
try {
InetAddress address = InetAddress.getLocalHost();// 获取的是本地的IP地址
localIp = address.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return localIp;
}
/**
* @Description 获取服务器IP地址
* @Title getMyIp
* @return
*/
public static String getServerIp() {
String serverIp = DEFAULT_SERVER_IP;// 本地IP,如果没有配置外网IP则返回它
try {
Enumeration
networkInterfaces = NetworkInterface.getNetworkInterfaces();
boolean flag = false;
while (networkInterfaces.hasMoreElements() && !flag) {
NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
Enumeration
inetAddresses = ni.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress nextElement = inetAddresses.nextElement(); if (!nextElement.isSiteLocalAddress() && !nextElement.isLoopbackAddress() && nextElement.getHostAddress().indexOf(":") == -1) {// 外网IP serverIp = nextElement.getHostAddress(); flag = true; break; } else if (nextElement.isSiteLocalAddress() && !nextElement.isLoopbackAddress() && nextElement.getHostAddress().indexOf(":") == -1) {// 内网IP serverIp = nextElement.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return serverIp; } /** * 获取远程客户端请求的IP地址 * * @param request * @return */ public static String getRemoteClientIp(HttpServletRequest request) { String clientIp = request.getHeader("x-forwarded-for"); if (StringUtil.isNotBlank(clientIp) && !"unknown".equalsIgnoreCase(clientIp)) { // 若有多级反向代理,取X-Forwarded-For中第一个非unknown的有效IP字符串 if (clientIp.contains(",")) { String[] split = clientIp.split(","); for (String ip : split) { if (!"unknown".equalsIgnoreCase(ip)) { return ip; } } } else { return clientIp; } } clientIp = request.getHeader("Proxy-Client-IP"); if (StringUtil.isNotBlank(clientIp) && !"unknown".equalsIgnoreCase(clientIp)) { return clientIp; } clientIp = request.getHeader("WL-Proxy-Client-IP"); if (StringUtil.isNotBlank(clientIp) && !"unknown".equalsIgnoreCase(clientIp)) { return clientIp; } clientIp = request.getRemoteAddr(); if (StringUtil.isNotBlank(clientIp) && !"unknown".equalsIgnoreCase(clientIp)) { return clientIp; } // 都为空时,取本地服务器IP return getLocalIp(); } public static void main(String[] args) { String localIp = getLocalIp(); String serverIp = getServerIp(); System.out.println(localIp); System.out.println(serverIp); } }
