29 lines
843 B
Java
29 lines
843 B
Java
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();
|
||
}
|
||
} |