JavaBeanBlog
JavaBeanBlog
BlogJava | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

get IP and Mac with java


 1import java.net.InetAddress;
 2import java.net.NetworkInterface;
 3import java.net.SocketException;
 4import java.net.UnknownHostException;
 5
 6public class getAddress {
 7    public static void main(String[] args) throws UnknownHostException {
            
            //get HOSTAddress
 8
        InetAddress thisIp = InetAddress.getLocalHost();
 9        try {
10            System.out.println("IP:" + thisIp.getHostAddress());
11        }
 catch (Exception e) {
12            e.printStackTrace();
13        }

14
15        try {
                //get  MAC By IP
17            NetworkInterface ni = NetworkInterface.getByInetAddress(thisIp);
18            if (ni != null) {
19                byte[] mac = ni.getHardwareAddress();
20                if (mac != null) {
21                    for (int k = 0; k < mac.length; k++) {
22                        System.out.format("%02X%s", mac[k],
23                                (k < mac.length - 1) ? "-" : "");
24                    }

25                }
 else {
26                    System.out.println("Address doesn't exist ");
27                }

28            }
 else {
29                System.out.println("address is not found.");
30            }

31        }
 catch (SocketException e) {
32            e.printStackTrace();
33        }

34    }

35}

36
发表于 2012-05-22 17:52 javaBean 阅读(183) 评论(0)  编辑  收藏
 
新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
 
随笔:0 文章:4 评论:0 引用:0
<2025年7月>
日一二三四五六
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

文章档案

  • 2012年6月 (1)
  • 2012年5月 (3)

搜索

  •  

最新评论


Powered by: 博客园
模板提供:沪江博客
Copyright ©2025 javaBean