当前位置:编程学习 > JSP >>

整理了sun网站java环境下获取网卡信息的资料

答案:    因为最近有程序需要在java环境下获取网卡ID,研究了SUN网站的相关资料(感谢令公子的连接)。
    测试整理了一下,分享给大家。
    思路是通过调用windows环境下的ipconfig命令和Linux环境下的ifconfig命令来获取网卡信息:
    分为四个程序文件:
test.java;NetworkInfo.java;WindowsNetworkInfo.java;LinuxNetworkInfo.java
//--------------test.java--------
package netcardinfo;

public class test {
  public test() {
  }
  public NetworkInfo nti = new WindowsNetworkInfo();
  public static void main(String[] args) {
    test test1 = new test();
    try {
       System.out.println("Network infos");
       System.out.println("Operating System:" + System.getProperty("os.name"));
       System.out.println("IP/Localhost:" + test1.nti.getLocalHost());
       System.out.println("MAC Address:" + test1.nti.getMacAddress());
       System.out.println("Domain:" + test1.nti.getNetworkDomain());
     }
     catch(Throwable t) {
       t.printStackTrace();
     }
  }
}
//---------end file---------

//-----------NetworkInfo.java----------
package netcardinfo;

import java.net.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public abstract class NetworkInfo{
      private static final String LOCALHOST = "localhost";
      public static final String NSLOOKUP_CMD = "nslookup";
      public abstract String parseMacAddress() throws ParseException;
/** Not too sure of the ramifications here, but it just doesn't seem right */
public String parseDomain() throws ParseException {  return parseDomain(LOCALHOST);    }
/** Universal entry for retrieving MAC Address */
public final static String getMacAddress() throws IOException {
     try {
        NetworkInfo info = getNetworkInfo();
        String mac = info.parseMacAddress();
        return mac;
         }
     catch(ParseException ex)
        { ex.printStackTrace();
        throw new IOException(ex.getMessage());
        }
   }
/** Universal entry for retrieving domain info */
public final static String getNetworkDomain() throws IOException{
    try {
       NetworkInfo info = getNetworkInfo();
       String domain = info.parseDomain();
       return domain;
       }
    catch(ParseException ex)
      {ex.printStackTrace();
       throw new IOException(ex.getMessage());
      }
    }
protected String parseDomain(String hostname) throws ParseException{
// get the address of the host we are looking for - verification
     java.net.InetAddress addy = null;
     try {
        addy = java.net.InetAddress.getByName(hostname);
      }
      catch ( UnknownHostException e ) {
        e.printStackTrace();
        throw new ParseException(e.getMessage(),0);
      }
// back out to the hostname - just validating
hostname = addy.getCanonicalHostName();
String nslookupCommand = NSLOOKUP_CMD + " " + hostname;
  // run the lookup command
String nslookupResponse = null;
     try {
        nslookupResponse = runConsoleCommand(nslookupCommand);
      }
      catch ( IOException e ){
      e.printStackTrace();
      throw new ParseException(e.getMessage(), 0);
      }
   StringTokenizer tokeit = new StringTokenizer(nslookupResponse,"\n",false);
      while( tokeit.hasMoreTokens() )
        { String line = tokeit.nextToken();
          if( line.startsWith("Name:")){line = line.substring(line.indexOf(":") + 1);
          line = line.trim();
          if( isDomain(line, hostname)){line = line.substring(hostname.length()+1);
          return line;
        }
    }
  }
  return "n.a.";
  }
  private static boolean    isDomain(String domainCandidate, String hostname)
    { Pattern domainPattern = Pattern.compile("[\\w-]+\\.[\\w-]+\\.[\\w-]+\\.[\\w-]+");
      Matcher m = domainPattern.matcher(domainCandidate);
      return m.matches() && domainCandidate.startsWith(hostname);
    }
  protected String runConsoleCommand(String command) throws IOException {
    Process p = Runtime.getRuntime().exec(command);
    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();
   return outputText;
}
/** Sort of like a factory... */
private static NetworkInfo getNetworkInfo() throws IOException {
   String os = System.getProperty("os.name");
   if(os.startsWith("Windows")) {
   return new WindowsNetworkInfo();
    }
   else if(os.startsWith("Linux")) {
   return new LinuxNetworkInfo();
   }
   else {
   throw new IOException("unknown operating system: " + os);
   }
}
protected String getLocalHost() throws ParseException {
   try {
   return java.net.InetAddress.getLocalHost().getHostAddress();
   

上一个:基于weblogic 的ejb 学习笔记(二)
下一个:记得以前有人需要得到磁盘的剩余空间的,我找到了

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,