u013385629
封雪冷凝
2014-12-26 06:44
采纳率: 51.4%
浏览 2.0k
已采纳

java 系统自动获取ip地址和mac地址

如何使用java代码自动获取计算机的ip地址和mac地址
求java代码

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • oyljerry
    oyljerry 2014-12-26 09:54
    已采纳
     public static String getMacAddressIP(String remotePcIP) {
      String str = "";
      String macAddress = "";
      try {
       Process pp = Runtime.getRuntime().exec("nbtstat -A " + remotePcIP);
       InputStreamReader ir = new InputStreamReader(pp.getInputStream());
       LineNumberReader input = new LineNumberReader(ir);
       for (int i = 1; i < 100; i++) {
        str = input.readLine();
        if (str != null) {
         if (str.indexOf("MAC Address") > 1) {
          macAddress = str.substring(
            str.indexOf("MAC Address") + 14, str.length());
          break;
         }
        }
       }
      } catch (IOException ex) {
      }
      return macAddress;
     }
    
    点赞 评论
  • baidu_24251857
    baidu_24251857 2014-12-26 07:13

    import java.net.*;public class NetInfo { public static void main(String[] args) { new NetInfo().say(); } public void say() { try { InetAddress i = InetAddress.getLocalHost(); System.out.println(i); //计算机名称和IP System.out.println(i.getHostName()); //名称 System.out.println(i.getHostAddress()); //只获得IP } catch(Exception e){e.printStackTrace();} }}

    点赞 评论

相关推荐