zoukankan      html  css  js  c++  java
  • Java根据ip地址获取Mac地址,Java获取Mac地址

    Java根据ip地址获取Mac地址,Java获取Mac地址

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

    蕃薯耀 2016年8月15日 11:07:55 星期一

    http://fanshuyao.iteye.com/

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    
    public class MacUtils {
    
    	/**
         * 根据IP地址获取mac地址
         * @param ipAddress 如:127.0.0.1
         * @return
         * @throws SocketException
         * @throws UnknownHostException
         */
        public static String getMac(String ipAddress) throws SocketException,
                UnknownHostException {
            String str = "";
            String macAddress = "";
            final String LOOPBACK_ADDRESS = "127.0.0.1";
            // 如果为127.0.0.1,则获取本地MAC地址。
            if (LOOPBACK_ADDRESS.equals(ipAddress)) {
                InetAddress inetAddress = InetAddress.getLocalHost();
                // 貌似此方法需要JDK1.6。
                byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
                // 下面代码是把mac地址拼装成String
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                    if (i != 0) {
                        sb.append("-");
                    }
                    // mac[i] & 0xFF 是为了把byte转化为正整数
                    String s = Integer.toHexString(mac[i] & 0xFF);
                    sb.append(s.length() == 1 ? 0 + s : s);
                }
                // 把字符串所有小写字母改为大写成为正规的mac地址并返回
                macAddress = sb.toString().trim().toUpperCase();
                return macAddress;
            } else {
                // 获取非本地IP的MAC地址
                try {
                    //System.out.println(ipAddress);
                    Process p = Runtime.getRuntime().exec("nbtstat -A " + ipAddress);
                    //System.out.println("===process=="+p);
                    InputStreamReader ir = new InputStreamReader(p.getInputStream());
                    BufferedReader br = new BufferedReader(ir);
                 
                    while ((str = br.readLine()) != null) {
                        if(str.indexOf("MAC") > 1 ){
                            macAddress = str.substring(str.indexOf("MAC") + 9, str.length());
                            macAddress = macAddress.trim();
                            //System.out.println("macAddress:" + macAddress);
                            break;
                        }
                    }
                    p.destroy();
                    br.close();
                    ir.close();
                } catch (IOException ex) {
                }
                return macAddress;
            }
        }
    
        public static void main(String[] args) {
            System.out.println(System.currentTimeMillis());
            try {
            	System.out.println(getMac("10.10.16.170"));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
            System.out.println(System.currentTimeMillis());
        }
        
        
    }

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

    蕃薯耀 2016年8月15日 11:07:55 星期一

    http://fanshuyao.iteye.com/

  • 相关阅读:
    数组作为方法参数时的一些意外情况
    pack://application:,,,/
    WPF 使用WinForm Chart控件
    WPF 后台绑定样式
    在转换为 UTC 时大于 DateTime.MaxValue 或小于 DateTime.MinValue 的 DateTime 值无法系列化为 JSON
    LINQ_to_SQL语法及实例大全
    C#编码好习惯,献给所有热爱c#的同学
    C#中OpenFileDialog的使用
    NET 2.0(C#)调用ffmpeg处理视频的方法
    SQLite Mysql 模糊查找(like)
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/6227105.html
Copyright © 2011-2022 走看看