package com.howin.util;import java.net.*; public class Ipconfig { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub InetAddress ia=null; try { ia=ia.getLocalHost(); String localname=ia.getHostName(); String localip=ia.getHostAddress(); System.out.println("本機名稱是:"+ localname); System.out.println("原生ip是 :"+localip); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } InetAddress ia1 = InetAddress.getLocalHost();//擷取本地IP對象 System.out.println("MAC ......... "+getMACAddress(ia1)); } //擷取MAC地址的方法 private static String getMACAddress(InetAddress ia)throws Exception{ //獲得網路介面對象(即網卡),並得到mac地址,mac地址存在於一個byte數組中。 byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); //下面代碼是把mac地址拼裝成String StringBuffer sb = new StringBuffer(); for(int i=0;i<mac.length;i++){ if(i!=0){ sb.append("-"); } //mac[i] & 0xFF 是為了把byte轉化為正整數 String s = Integer.toHexString(mac[i] & 0xFF); System.out.println("--------------"); System.err.println(s); sb.append(s.length()==1?0+s:s); } //把字串所有小寫字母改為大寫成為正規的mac地址並返回 return sb.toString().toUpperCase(); } }