Skip to content

Commit 57fe422

Browse files
committed
获取主机信息
1 parent b48a015 commit 57fe422

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
* [使用Jetty实现Http Server Mock作单元测试](src/cn/aofeng/demo/jetty)
6060
* [使用WireMock实现Http Server Mock作单元测试](src/cn/aofeng/demo/wiremock)
6161

62+
##其他
63+
* [获取主机信息](src/cn/aofeng/demo/misc/GetHostInfo.java)
6264

6365
#Java高级
6466
##网络与I/O
@@ -68,6 +70,9 @@
6870
* [NIO 缓冲区](src/cn/aofeng/demo/nio/BufferIO.java)
6971
* [NIO 内存映射](src/cn/aofeng/demo/nio/MemoryMapper.java)
7072
* [NioEchoServer](src/cn/aofeng/demo/nio/NioEchoServer.java)
73+
* [netty4.x-DiscardServer]()
74+
* [netty4.x-DiscardServer]()
75+
* [netty4.x-DiscardServer]()
7176

7277
###文章
7378
* [NIO - 入门篇(笔记)](http://aofengblog.blog.163.com/blog/static/6317021200892145321884/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cn.aofeng.demo.misc;
2+
3+
import java.net.InetAddress;
4+
import java.net.NetworkInterface;
5+
import java.net.SocketException;
6+
import java.net.UnknownHostException;
7+
import java.util.Enumeration;
8+
import java.util.Map;
9+
import java.util.Map.Entry;
10+
import java.util.Properties;
11+
import java.util.Set;
12+
13+
/**
14+
* 获取本机IP和主机名以及Java环境信息。
15+
*
16+
* @author <a href="mailto:[email protected]">聂勇</a>
17+
*/
18+
public class GetHostInfo {
19+
20+
/**
21+
* @param args
22+
* @throws UnknownHostException
23+
* @throws SocketException
24+
*/
25+
public static void main(String[] args) throws UnknownHostException, SocketException {
26+
InetAddress address = InetAddress.getLocalHost();
27+
System.out.println("计算机名:" + address.getHostName());
28+
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
29+
while (nis.hasMoreElements()) {
30+
StringBuilder buffer = new StringBuilder();
31+
NetworkInterface ni = nis.nextElement();
32+
buffer.append("网卡:").append(ni.getName());
33+
buffer.append(" 绑定IP:");
34+
Enumeration<InetAddress> ias = ni.getInetAddresses();
35+
int count = 0;
36+
while (ias.hasMoreElements()) {
37+
InetAddress ia = ias.nextElement();
38+
if (count > 0) {
39+
buffer.append(", ");
40+
}
41+
buffer.append(ia.getHostAddress());
42+
}
43+
System.out.println(buffer.toString());
44+
}
45+
46+
System.out.println("Java环境信息:");
47+
System.out.println("---------------------------------------------------:");
48+
Properties pros = System.getProperties();
49+
Set<Entry<Object, Object>> javaEnums = pros.entrySet();
50+
for (Entry<Object, Object> entry : javaEnums) {
51+
System.out.println(entry.getKey() + " : " + entry.getValue());
52+
}
53+
System.out.println("");
54+
System.out.println("系统环境信息:");
55+
System.out.println("---------------------------------------------------:");
56+
Map<String, String> envs = System.getenv();
57+
Set<Entry<String, String>> envSet = envs.entrySet();
58+
for (Entry<String, String> entry : envSet) {
59+
System.out.println(entry.getKey() + " : " + entry.getValue());
60+
}
61+
}
62+
63+
}

0 commit comments

Comments
 (0)