免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1487 | 回复: 0
打印 上一主题 下一主题

Java得到网卡物理地址(Windows Linux) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-06 15:18 |只看该作者 |倒序浏览

                                                               
推广组1_2008-08-07
关键字: java 网卡物理地址 ip mac
代码如下:

               
               
                   1. import java.io.BufferedReader;  
   2. import java.io.IOException;  
   3. import java.io.InputStreamReader;  
   4. import java.util.Properties;  
   5. import java.util.logging.Level;  
   6. import java.util.logging.Logger;  
   7.   
   8. public class Test {  
   9.   
  10.     public static String getMACAddress() {  
  11.   
  12.          String address = "";  
  13.          String os = System.getProperty("os.name");  
  14.          System.out.println(os);  
  15.         if (os != null) {  
  16.             if (os.startsWith("Windows")) {  
  17.                 try {  
  18.                      ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");  
  19.                      Process p = pb.start();  
  20.                      BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  
  21.                      String line;  
  22.                     while ((line = br.readLine()) != null) {  
  23.                         if (line.indexOf("Physical Address") != -1) {  
  24.                             int index = line.indexOf(":");  
  25.                              address = line.substring(index + 1);  
  26.                             break;  
  27.                          }  
  28.                      }  
  29.                      br.close();  
  30.                     return address.trim();  
  31.                  } catch (IOException e) {  
  32.                       
  33.                  }  
  34.              }else if(os.startsWith("Linux")){  
  35.                 try {  
  36.                      ProcessBuilder pb = new ProcessBuilder("ifconfig");  
  37.                      Process p = pb.start();  
  38.                      BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  
  39.                      String line;  
  40.                     while((line=br.readLine())!=null){  
  41.                         int index=line.indexOf("硬件地址");  
  42.                         if(index!=-1){  
  43.                              address=line.substring(index+4);  
  44.                             break;  
  45.                          }  
  46.                      }  
  47.                      br.close();  
  48.                     return address.trim();  
  49.                  } catch (IOException ex) {  
  50.                      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);  
  51.                  }  
  52.                  
  53.              }  
  54.          }  
  55.         return address;  
  56.      }  
  57.   
  58.     public static void main(String[] args) {  
  59.          System.out.println("" + Test.getMACAddress());  
  60.      }  
  61. }  

               
                 
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/75329/showart_1110787.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP