獲取MAC地址

	/**
	 * 獲取本機mac地址
	 * @return
	 */
    public static String getMac() {
		StringBuffer sb = new StringBuffer("");
		try {
			InetAddress ia = InetAddress.getLocalHost();
			System.out.println(ia);
			byte[] mac = NetworkInterface .getByInetAddress(ia).getHardwareAddress();
			for (int i = 0; i < mac.length; i++) {
				if(i != 0) {
					sb.append("-");
				}
				//字節轉換爲整數
				int temp = mac[i] & 0xff;
				String str = Integer.toHexString(temp);
				if(str.length() == 1) {
					sb.append("0" + str.toUpperCase());
				}else {
					sb.append(str.toUpperCase());
				}
			}
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return sb.toString();
	} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章