Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53554
  • 博文数量: 10
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-05 21:59
文章分类

全部博文(10)

文章存档

2011年(1)

2008年(9)

我的朋友
最近访客

分类: Java

2008-08-06 15:18:53


关键字: 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. }

阅读(1206) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~