Java常用工具类

1 篇文章 0 订阅
1 篇文章 0 订阅

记录几个常用的java工具类

package cn.sh.ideal.web.util;

import cn.sh.ideal.system.entity.SystemConfig;
import cn.sh.ideal.system.service.ISystemConfigService;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;

/**
 * 常用工具类
 */
@Component("webIpUtil")
public class WebIpUtil {
    @Resource(name = "configService")
    ISystemConfigService configService;
    /**
     * 判断当前服务器ip与配置ip是否一致
     * 判断当前服务器文件路径是否包含配置文件名
     */
    public String configCheckLocal() throws SocketException, UnknownHostException {
        //服务器文件路径 和 服务器ip
        String rootPath = getClass().getResource("/").getFile();
        String serverIp = getLocalIP();
        SystemConfig pathConfig = configService.findByKey("SERVER_ROOT_PATH");
        JSONObject jsonObject = JSONObject.fromObject(pathConfig.getCfgValue());
        for (Object key : jsonObject.keySet()) {
            if (rootPath.contains(jsonObject.get(key).toString())) {
                return serverIp + ":" + key;
            }
        }
        return null;
    }

    /**
     * 获取本地IP地址
     *
     * @throws SocketException
     */
    public static String getLocalIP() throws UnknownHostException, SocketException {
        if (isWindowsOS()) {
            return InetAddress.getLocalHost().getHostAddress();
        } else {
            return getLinuxLocalIp();
        }
    }

    /**
     * 判断操作系统是否是Windows
     *
     * @return
     */
    public static boolean isWindowsOS() {
        boolean isWindowsOS = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindowsOS = true;
        }
        return isWindowsOS;
    }

    /**
     * 获取Linux下的IP地址
     *
     * @return IP地址
     * @throws SocketException
     */
    private static String getLinuxLocalIp() throws SocketException {
        String ip = "";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                String name = intf.getName();
                if (!name.contains("docker") && !name.contains("lo")) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            String ipaddress = inetAddress.getHostAddress().toString();
                            if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
                                ip = ipaddress;
                            }
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            ip = "127.0.0.1";
            ex.printStackTrace();
        }
        return ip;
    }

    public static List<String> getEndPoints() {

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objs = null;
        ArrayList<String> endPoints = null;
        try {
            objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
            String hostname = InetAddress.getLocalHost().getHostName();
            InetAddress[] addresses = InetAddress.getAllByName(hostname);
            endPoints = Lists.newArrayList();
            for (Iterator<ObjectName> i = objs.iterator(); i.hasNext(); ) {
                ObjectName obj = i.next();
                String port = obj.getKeyProperty("port");
                endPoints.add(port);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return endPoints;
    }
}

测试工具类:

public static void main(String[] args) {
        try {
            String ip=WebIpUtil.getLocalIP();
            Boolean isWindow=WebIpUtil.isWindowsOS();
            String linuxIp=WebIpUtil.getLinuxLocalIp();
            System.out.println("ip:"+ip+"   isWindow:"+isWindow+"   linuxIp:"+linuxIp);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

输出:ip:10.88.233.58   isWindow:true   linuxIp:10.88.233.58

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值