获取IP地址
public static String getMachineIP() {
List<String> ipList = new ArrayList<String>();
try {
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress inetAddress;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
inetAddress = (InetAddress) addresses.nextElement();
if (inetAddress != null && inetAddress.isSiteLocalAddress()) {
ipList.add(inetAddress.getHostAddress());
}
}
}
} catch (SocketException ignore) {
}
return ipList.size() > 0 ? ipList.get(0) : InetAddress.getLocalHost().getHostAddress();
}
获取端口号
/**
* 获取tomcat服务端口号
*
* @param isHttps 否为https端口
*/
private static String getServerPort(boolean isHttps) throws AttributeNotFoundException,
InstanceNotFoundException, MBeanException, ReflectionException {
MBeanServer mBeanServer = null;
if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
}
if (mBeanServer == null) {
logger.debug("调用findMBeanServer查询到的结果为null");
return "";
}
Set<ObjectName> names;
try {
names = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
} catch (Exception e) {
return "";
}
Iterator<ObjectName> it = names.iterator();
ObjectName OName;
while (it.hasNext()) {
OName = it.next();
String protocol = (String) mBeanServer.getAttribute(OName, "protocol");
String scheme = (String) mBeanServer.getAttribute(OName, "scheme");
Boolean secureValue = (Boolean) mBeanServer.getAttribute(OName, "secure");
Boolean SSLEnabled = (Boolean) mBeanServer.getAttribute(OName, "SSLEnabled");
if (SSLEnabled != null && SSLEnabled) {// tomcat6开始用SSLEnabled
secureValue = true;// SSLEnabled=true但secure未配置的情况
scheme = "https";
}
if (protocol != null && ("HTTP/1.1".equals(protocol) || protocol.contains("http"))) {
if (isHttps && "https".equals(scheme) && secureValue) {
return (mBeanServer.getAttribute(OName, "port")).toString();
} else if (!isHttps && !"https".equals(scheme) && !secureValue) {
return (mBeanServer.getAttribute(OName, "port")).toString();
}
}
}
return "";
}
或者
public static final int getTomcatPort(){
try{
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
Service[] services = server.findServices();
for (Service service : services) {
Connector connector = Iterables.find(Arrays.asList(service.findConnectors()), new Predicate<Connector>() {
@Override
public boolean apply(Connector input) {
ProtocolHandler protocolHandler = input.getProtocolHandler();
return protocolHandler instanceof Http11AprProtocol
|| protocolHandler instanceof Http11NioProtocol;
}
});
return connector.getPort();
}
}catch (Exception e){
LOGGER.error("获取tomcat端口失败",e);
}
return 8080;
}
者或
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");