java获取网卡的mac地址

来源:互联网 发布:windows 10桌面 编辑:程序博客网 时间:2024/04/28 18:58

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.net.NetworkInterface;public class MacAddressAPI {    /**     * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.     */    public static String getOSName() {        return System.getProperty("os.name").toLowerCase();    }    /**     * 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取. 如果有特殊系统请继续扩充新的取mac地址方法.     *      * @return mac地址     */    public static String getUnixMACAddress() {        String mac = null;        BufferedReader bufferedReader = null;        Process process = null;        try {            // linux下的命令,一般取eth0作为本地主网卡            process = Runtime.getRuntime().exec("ifconfig eth0");            // 显示信息中包含有mac地址信息            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));            String line = null;            int index = -1;            while ((line = bufferedReader.readLine()) != null) {                // 寻找标示字符串[hwaddr]                index = line.toLowerCase().indexOf("hwaddr");                if (index >= 0) {// 找到了                    // 取出mac地址并去除2边空格                    mac = line.substring(index + "hwaddr".length() + 1).trim();                    break;                }            }        }        catch (IOException e) {            System.out.println("unix/linux方式未获取到网卡地址");        }        finally {            try {                if (bufferedReader != null) {                    bufferedReader.close();                }            }            catch (IOException e1) {                e1.printStackTrace();            }            bufferedReader = null;            process = null;        }        return mac;    }    /**     * 获取widnows网卡的mac地址.     *      * @return mac地址     */    public static String getWindowsMACAddress() {        String mac = null;        BufferedReader bufferedReader = null;        Process process = null;        try {            // windows下的命令,显示信息中包含有mac地址信息            process = Runtime.getRuntime().exec("ipconfig /all");            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));            String line = null;            int index = -1;            while ((line = bufferedReader.readLine()) != null) {                // 寻找标示字符串[physical                index = line.toLowerCase().indexOf("physical address");                if (index >= 0) {// 找到了                    index = line.indexOf(":");// 寻找":"的位置                    if (index >= 0) {                        // 取出mac地址并去除2边空格                        mac = line.substring(index + 1).trim();                    }                    break;                }            }        }        catch (IOException e) {            System.out.println("widnows方式未获取到网卡地址");        }        finally {            try {                if (bufferedReader != null) {                    bufferedReader.close();                }            }            catch (IOException e1) {                e1.printStackTrace();            }            bufferedReader = null;            process = null;        }        return mac;    }    /**     * windows 7 专用 获取MAC地址     *      * @return     * @throws Exception     */    public static String getWindows7MACAddress() {        StringBuffer sb = new StringBuffer();        try {            // 获取本地IP对象            InetAddress ia = InetAddress.getLocalHost();            // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();            // 下面代码是把mac地址拼装成String            for (int i = 0; i < mac.length; i++) {                // mac[i] & 0xFF 是为了把byte转化为正整数                String s = Integer.toHexString(mac[i] & 0xFF);                sb.append(s.length() == 1 ? 0 + s : s);            }        }        catch (Exception ex) {            System.out.println("windows 7方式未获取到网卡地址");        }        return sb.toString();    }    /**     * 获取MAC地址     *      * @param argc     *            运行参数.     * @throws Exception     */    public static String getMACAddress() {        // windows        String mac = getWindowsMACAddress();        // windows7        if (isNull(mac)) {            mac = getWindows7MACAddress();        }        // unix        if (isNull(mac)) {            mac = getUnixMACAddress();        }        if (!isNull(mac)) {            mac = mac.replace("-", "");        }        else {            mac = "ABCDEFGHIJ";        }        return mac.toLowerCase();    }    public static boolean isNull(Object strData) {        if (strData == null || String.valueOf(strData).trim().equals("")) {            return true;        }        return false;    }    public static void main(String[] args) {        System.out.println(getWindows7MACAddress());    }}



转http://blog.csdn.net/zl544434558/article/details/21701757

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 河北省监理员证怎么办 买的商铺烂尾了怎么办 钢表带被磨花了怎么办 资料员到期了怎么办 八大员挂靠社保怎么办 安许证三类人员不足了怎么办 考试准考证号写错了怎么办 科目一失约两次怎么办 钢筋送检两次不合格怎么办 公路原材料抽检不合格怎么办 混凝土回弹强度不合格怎么办 毕业证照片太丑怎么办 政审时找不到档案怎么办 劳动解除书开不出来怎么办 双流办健康证怎么办 户口本人数满了怎么办 二建有效期到了怎么办 二级建造师到期怎么办 网上选车牌号后怎么办 政府拖欠水利工程款怎么办 重庆造价员到期了怎么办 枕大神经发炎怎么办 塑钢推拉窗下沉怎么办 网上买东西数量不够怎么办 淘宝买东西数量不够怎么办 康佳遥控器坏了怎么办 电视机频幕脏了怎么办 极米遥控器失灵怎么办 电视遥控坏了怎么办 小米手机触摸屏失灵怎么办 oppo手机触摸屏失灵怎么办 苹果手机屏幕没反应怎么办 苹果6sp换屏卡顿怎么办 苹果4屏幕失灵怎么办 苹果手机老是卡怎么办 苹果手机卡机了怎么办 苹果手机卡屏了怎么办 苹果8手机老卡机怎么办 苹果手机不开机怎么办 手机开机后黑屏怎么办 苹果x手机黑屏怎么办