转自: https://blog.csdn.net/QiaoRui_/article/details/83106485
描述:
我们有时候需要获取当前服务的IP和端口
实现:
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.util.Set;
public class IPPortUtil {
/**
* @return
* @throws MalformedObjectNameException
* 获取当前机器的端口号
*/
public static String getLocalPort() throws MalformedObjectNameException {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String port = objectNames.iterator().next().getKeyProperty("port");
return port;
}
/**
* @return
* 获取当前机器的IP
*/
public static String getLocalIP() {
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
}
byte[] ipAddr = addr.getAddress();
String ipAddrStr = "";
for (int i = 0; i < ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i] & 0xFF;
}
return ipAddrStr;
}
}
---------------------
作者:QiaoRui_
来源:CSDN
原文:https://blog.csdn.net/QiaoRui_/article/details/83106485
版权声明:本文为博主原创文章,转载请附上博文链接!