2025-01-27 21:47:33 +08:00

29 lines
843 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.upchina.common.util;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Component
public class WebServerInfo {
@Resource
private WebServerApplicationContext webServerApplicationContext;
public String getServerIp() {
// 获取本地的 IP 地址Spring Boot 默认会绑定到 0.0.0.0(所有网络接口)
try {
InetAddress inetAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return inetAddress.getHostAddress();
}
public int getServerPort() {
return webServerApplicationContext.getWebServer().getPort();
}
}