在我们的项目中,我们使用 JMX 来获取所有配置信息。它需要几个步骤,因为它就像在 server.xml 文件中向下导航这个链接有一些信息: http: //oss.wxnet.org/mbeans.html
如果你想要的只是 IP,这可能是矫枉过正,但我想我会把它扔在那里。
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> theConnectors = mbeanServer.queryNames(
new ObjectName("Catalina:type=Connector,*"),
null);
if (theConnectors != null)
{
for (ObjectName nextConnectorName : theConnectors)
{
InetAddress theInetAddress = (InetAddress) mbeanServer.getAttribute(
nextConnectorName,
"address");
if (theInetAddress != null)
{
ipAddress = theInetAddress.getHostAddress();
}
if (!StringUtil.isEmpty(ipAddress))
{
// found the IP address
break;
}
}
}