---
title: "用java获取系统网卡的物理地址"
author: "dengchangqing"
date: "2007-11-29T10:35:02Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-11-29T10:35:02Z"
id: 144756
url: "https://changing.iteye.com/blog/144756"
---

为了保证用户名的安全，将用户名和该机器的网卡物理地址进行绑定，将其保存至session中，登陆的时候检查该用户的session是否是与本机器网卡的物理地址相匹配。
package com.sdtrip.MacDemo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class LocalMacAddress
{

	
	public static void main(String args)
	{
		String MAC = LocalMacAddress.getMacOnWindow();
		 System.out.println(MAC);
		 
		
    }
private static String getMacOnWindow() {
    String s = "";
   
    try {
        String s1 = "ipconfig /all";
        Process process = Runtime.getRuntime().exec(s1);
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String nextLine;
        for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
            nextLine = bufferedreader.readLine();
            if (line.indexOf("Physical Address") <= 0) {
                continue;
            }
            int i = line.indexOf("Physical Address") + 36;
            s = line.substring(i);
            break;
        }
        bufferedreader.close();
        process.waitFor();
    } catch (Exception exception) {
        s = "";
    }
    return s.trim();
	}

}