Pixiv - KiraraShss
296 字
2 分钟
✨Java 获取以太网IP
Java 获取以太网IP
遍历获取以eth开头的网卡
package com.lds.utils;
import cn.hutool.log.StaticLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
/**
* <strong style='color:purple;'>Created with IntelliJ IDEA.<hr>
* <strong style='color:orange;'>Author: lds<hr>
* <strong style='color:yellow;'>Date: 2021/9/1 10:01<hr>
* <strong style='color:blue;'>Class: com.lds.util<hr>
* <strong style='color:indigo;'>Project: demo<hr>
* <strong style='color:red;'>Description: 获取当前IP及端口<hr>
*/
@Slf4j
@Component
public class IPUtils {
private static String serverPort;
@Value("${server.port}")
private String port;
@PostConstruct
public void setServerPort() {
serverPort = port;
StaticLog.info("port:" + serverPort);
}
/**
* 获取当前服务的ip和端口
*
* @param
* @author: lds
* @date: 2021/9/1 10:03
* @return: {@link String}
*/
public static String getServerIpAndPort() {
InetAddress address = null;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface ni : Collections.list(interfaces)) {
if (ni.isUp() && !ni.isLoopback() && ni.getName().startsWith("eth")) {
Enumeration<InetAddress> addresses = ni.getInetAddresses();
for (InetAddress inetAddress : Collections.list(addresses)) {
if (inetAddress.isSiteLocalAddress()) {
log.info("Ethernet IP Address: {}", inetAddress.getHostAddress());
address = inetAddress;
}
}
}
}
} catch (SocketException e) {
log.error("", e);
}
return address.getHostAddress() + ":" + IPUtils.serverPort;
}
/**
* 获取当前服务的ip和端口
*
* @param
* @author: lds
* @date: 2021/9/1 10:03
* @return: {@link String}
*/
public static String getServerIpAndPort(InetAddress address) {
return address.getHostAddress() + ":" + IPUtils.serverPort;
}
public static String getRealIp(HttpServletRequest request) {
String ipAddress = request.getHeader("X-Real-IP");
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("X-Forwarded-For");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
return ipAddress;
}
}
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
最后更新于 2024-07-23,距今已过 623 天
部分内容可能已过时
北港不夏