JavaBeanBlog
JavaBeanBlog
BlogJava
|
首页
|
发新随笔
|
发新文章
|
联系
|
聚合
|
管理
get IP and Mac with java
1
import
java.net.InetAddress;
2
import
java.net.NetworkInterface;
3
import
java.net.SocketException;
4
import
java.net.UnknownHostException;
5
6
public
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月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
留言簿
给我留言
查看公开留言
查看私人留言
文章档案
2012年6月 (1)
2012年5月 (3)
搜索
最新评论