java 获取Window机器 中英文机器 IP地址

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mbh12333/article/details/81537105
private static String getIPAddress() throws IOException {
        Process pro = Runtime.getRuntime().exec("ipconfig");
        BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream(),"GBK"));
        List<String> rowList = new ArrayList<String>();
        String temp;
        while((temp = br.readLine()) != null){
            rowList.add(temp);
        }
        String gateWays = null,
                ip = null;
        for (String string : rowList) {
            if(string.contains("IPv4 地址") || string.contains("IPv4 Address")){
                Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
                if(mc.find()){
                    ip = mc.group();
                }
            }
            if(string.contains("默认网关") || string.contains("Default Gateway")){
                Matcher mc = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(string);
                if(mc.find()){
                    gateWays = mc.group();
                    break;
                }
            }
        }
        System.out.println("gateways:"+gateWays+"   IP:"+ip);
        return ip;
    }

猜你喜欢

转载自blog.csdn.net/mbh12333/article/details/81537105
今日推荐