获取服务器Ip端口号

获取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();
}

或者

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<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");



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值