您的位置:首页 > 编程语言 > Java开发

java jsp 获取电脑MAC地址

2013-06-28 23:45 260 查看
package filters;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.LineNumberReader;

public class GetMACAddress {

    public String getMACAddress(String ipAddress) {

        String str = "", strMAC = "", macAddress = "";

        try {

            Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);

            InputStreamReader ir = new InputStreamReader(pp.getInputStream());

            LineNumberReader input = new LineNumberReader(ir);

            for (int i = 1; i < 100; i++) {

                str = input.readLine();

                if (str != null) {

                    if (str.indexOf("MAC Address") > 1) {

                        strMAC = str.substring(str.indexOf("MAC Address") + 14,

                                str.length());

                        break;

                    }

                }

            }

        } catch (IOException ex) {

            return "Can't Get MAC Address!";

        }

        //

        if (strMAC.length() < 17) {

            return "Error!";

        }

        macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)

                + ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)

                + ":" + strMAC.substring(12, 14) + ":"

                + strMAC.substring(15, 17);

        //

        return macAddress;

    }

    public static void main(String[] args) {

        GetMACAddress getMACAddress = new GetMACAddress();

        System.out.println(getMACAddress.getMACAddress("192.168.0.100")); // 获得该ip地址的mac地址

    }

    public static String procAll(String str) {

        return procStringEnd(procFirstMac(procAddress(str)));

    }

    public static String procAddress(String str) {

        int indexof = str.indexOf("Physical Address");

        if (indexof > 0) {

            return str.substring(indexof, str.length());

        }

        return str;

    }

    public static String procFirstMac(String str) {

        int indexof = str.indexOf(":");

        if (indexof > 0) {

            return str.substring(indexof + 1, str.length()).trim();

        }

        return str;

    }

    public static String procStringEnd(String str) {

        int indexof = str.indexOf("\r");

        if (indexof > 0) {

            return str.substring(0, indexof).trim();

        }

        return str;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: