java根據本地Ip擷取mac地址

來源:互聯網
上載者:User

標籤:

 1 import java.net.InetAddress; 2 import java.net.NetworkInterface; 3 import java.net.SocketException; 4 import java.net.UnknownHostException; 5  6 public class MacAddress { 7  8     /** 9      * @param args10      * @throws UnknownHostException 11      * @throws SocketException 12      */13     public static void main(String[] args) throws UnknownHostException, SocketException {14 15         InetAddress ia = InetAddress.getLocalHost();16         System.out.println(ia);17         getLocalMac(ia);18     }19     private static void getLocalMac(InetAddress ia) throws SocketException {20         // TODO Auto-generated method stub21         //擷取網卡,擷取地址22         byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();23         24         System.out.println("mac數組長度:"+mac.length);25         StringBuffer sb = new StringBuffer("");26         for(int i=0; i<mac.length; i++) {27             if(i!=0) {28                 sb.append("-");29             }30             //位元組轉換為整數31             int temp = mac[i]&0xff;32             String str = Integer.toHexString(temp);33             System.out.println("每8位:"+str);34             if(str.length()==1) {35                 sb.append("0"+str);36             }else {37                 sb.append(str);38             }39         }40         System.out.println("本機MAC地址:"+sb.toString().toUpperCase());41     }42 }

本來想通過寫web程式通過獲得使用者ip然後得到使用者的網卡地址,這樣可以做某些業務了。。。結果發現web暫時沒有實現方法,找另外方法去了

java根據本地Ip擷取mac地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.