获取服务器Ip端口号

获取IP地址

public static String getMachineIP() {
    List ipList = new ArrayList();
    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();
}

或者

public static String getLocalIP() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        byte[] ipAddr = addr.getAddress();
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
        // System.out.println(ipAddrStr);
        return ipAddrStr;
    }

 

获取端口号

/**
 * 获取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 names;
    try {
        names = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
    } catch (Exception e) {
        return "";
    }
    Iterator 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() {
                @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 objectNames = beanServer.queryNames(
         new ObjectName("*:type=Connector,*"),
         Query.match(Query.attr("protocol"),Query.value("HTTP/1.1")));
String port = objectNames.iterator().next().getKeyProperty("port");



原文作者链接
https://www.jianshu.com/p/71352e15966f
 

你可能感兴趣的:(获取服务器Ip端口号)