Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 2cd52e3

Browse files
committed
增加网络工具类
1 parent 8963d0b commit 2cd52e3

File tree

1 file changed

+69
-0
lines changed
  • src/main/java/org/suren/autotest/web/framework/util

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2007 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.suren.autotest.web.framework.util;
18+
19+
import java.net.Inet6Address;
20+
import java.net.InetAddress;
21+
import java.net.InterfaceAddress;
22+
import java.net.NetworkInterface;
23+
import java.net.SocketException;
24+
import java.util.Enumeration;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
/**
30+
* 网络相关的工具类
31+
* @author suren
32+
* @date 2017年5月7日 下午4:33:43
33+
*/
34+
public class NetUtil
35+
{
36+
/**
37+
* @return 所有的对外ip,没有的话返回空集合
38+
*/
39+
public static Map<String, String> allIP()
40+
{
41+
Map<String, String> allIP = new HashMap<String, String>();
42+
try
43+
{
44+
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
45+
while (netInterfaces.hasMoreElements())
46+
{
47+
NetworkInterface netInter = (NetworkInterface) netInterfaces.nextElement();
48+
49+
List<InterfaceAddress> address = netInter.getInterfaceAddresses();
50+
for(InterfaceAddress interAddr : address)
51+
{
52+
InetAddress addr = interAddr.getAddress();
53+
if(addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr instanceof Inet6Address)
54+
{
55+
continue;
56+
}
57+
58+
allIP.put(netInter.getDisplayName(), addr.getHostAddress());
59+
}
60+
}
61+
}
62+
catch (SocketException e)
63+
{
64+
e.printStackTrace();
65+
}
66+
67+
return allIP;
68+
}
69+
}

0 commit comments

Comments
 (0)