⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getmacaddress.java

📁 这个是我们做土地局的一个项目。感觉不错。欢迎下载
💻 JAVA
字号:
package myprojects.getmacaddress;

import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;

public class GetMacAddress {
	
	
	///
	private static String mac;
	
/*	public static void main(String args[]) {
	  try {
			System.out.println("Starting GetMacAddress...");
			GetMacAddress GetMacAddress1 = new GetMacAddress();
			GetMacAddress1.getMacAddress();
				System.out.println("Starting GetMacAddress...");
		//	GetMacAddress1.getMac();
			System.out.println(mac);
	  } catch (Exception e) {
	  	System.out.println(e);
	  }
	}*/
		
	public String getMac(){
	//	System.out.println("Starting GetMacAddress...");
	try {
		GetMacAddress GetMacAddress1 = new GetMacAddress();
			GetMacAddress1.getMacAddress();
			
		//	GetMacAddress1.getMac();
	              return mac;
	              } catch (Exception e) {
	  	System.out.println(e);
	  	return "";
	  }


}
	///
	

   public   void getMacAddress() throws IOException {
   		try {
        //return windowsParseMacAddress(windowsRunIpConfigCommand());
        mac=windowsParseMacAddress(windowsRunIpConfigCommand());
        //return mac;
   		} catch (java.text.ParseException e) {
   			System.out.println(e);
   		//	return "";
   		}
   }
   private final static String windowsParseMacAddress(String 

ipConfigResponse) throws ParseException {
       String localHost = null;
       try {
           localHost = InetAddress.getLocalHost().getHostAddress();
       } catch (java.net.UnknownHostException ex) {
           ex.printStackTrace();
           throw new ParseException(ex.getMessage(), 0);
       }
       
       
       StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, 

"\n");
       String lastMacAddress = null;
       while (tokenizer.hasMoreTokens()) {
           String line = tokenizer.nextToken().trim();
           // see if line contains IP address
           if (line.endsWith(localHost) && lastMacAddress != null) {
			   			System.out.println( 

"macAddress = " + lastMacAddress );
              return lastMacAddress;
           }
           // see if line contains MAC address
           int macAddressPosition = line.indexOf(":");
           if (macAddressPosition <= 0)
               continue;
           String macAddressCandidate = line.substring(macAddressPosition + 

1).trim();
           if (windowsIsMacAddress(macAddressCandidate)) {
               lastMacAddress = macAddressCandidate;
               continue;
           }
       }
       ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigResponse + "]", 0);
       ex.printStackTrace();
       throw ex;
   }
   private final static boolean windowsIsMacAddress(String 

macAddressCandidate) {
       // TODO: use a smart regular expression
       if (macAddressCandidate.length() != 17)
           return false;
       return true;
   }
   private final static String windowsRunIpConfigCommand() throws IOException 

{
       Process p = Runtime.getRuntime().exec("ipconfig /all");
       InputStream stdoutStream = new BufferedInputStream(p.getInputStream

());
       StringBuffer buffer= new StringBuffer();
       for (;;) {
           int c = stdoutStream.read();
           if (c == -1)
               break;
           buffer.append((char)c);
       }
       String outputText = buffer.toString();
       stdoutStream.close();
	   System.out.println("outputText = " + outputText );
       return outputText;
   }
   

 

/*	public static void main(String args[]) {
	  try {
			System.out.println("Starting GetMacAddress...");
			GetMacAddress GetMacAddress1 = new GetMacAddress();
			GetMacAddress1.getMacAddress();
	  } catch (Exception e) {
	  	System.out.println(e);
	  }
	}*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -