有些机器有许多虚拟的网卡,获取IP地址时会出现一些意外,所以需要一些验证:
1 // 获取mac地址
2 public static String getMacAddress() {
3 try {
4 Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
5 byte[] mac = null;
6 while (allNetInterfaces.hasMoreElements()) {
7 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
8 if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
9 continue;
10 } else {
11 mac = netInterface.getHardwareAddress();
12 if (mac != null) {
13 StringBuilder sb = new StringBuilder();
14 for (int i = 0; i < mac.length; i++) {
15 sb.append(String.format("%02X%s", mac, (i < mac.length - 1) ? "-" : ""));
16 }
17 if (sb.length() > 0) {
18 return sb.toString();
19 }
20 }
21 }
22 }
23 } catch (Exception e) {
24 _logger.error("MAC地址获取失败", e);
25 }
26 return "";
27 }
28
29 // 获取ip地址
30 public static String getIpAddress() {
31 try {
32 Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
33 InetAddress ip = null;
34 while (allNetInterfaces.hasMoreElements()) {
35 NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
36 if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
37 continue;
38 } else {
39 Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
40 while (addresses.hasMoreElements()) {
41 ip = addresses.nextElement();
42 if (ip != null && ip instanceof Inet4Address) {
43 return ip.getHostAddress();
44 }
45 }
46 }
47 }
48 } catch (Exception e) {
49 _logger.error("IP地址获取失败", e);
50 }
51 return "";
52 }
以上的代码中
netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()
能很好地把一些非物理网卡或无用网上过滤掉,然后再取网上的IPV4地址即可。
说到这里,还有一些常用的:
1、获取当前机器的操作系统
public final static String WIN_OS = "WINDOWS";
public final static String MAC_OS = "MAC";
public final static String LINUX_OS = "LINUX";
public final static String OTHER_OS = "OTHER";
public static String getOS() {
if (SystemUtils.IS_OS_WINDOWS){
return WIN_OS;
}
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX){
return MAC_OS;
}
if (SystemUtils.IS_OS_UNIX){
return LINUX_OS;
}
return OTHER_OS;
}
2、设置HTTP访问代理
1 /**
2 * 设置http代理
3 */
4 public static void setHttpProxy() {
5 Properties prop = System.getProperties();
6 // 设置http访问要使用的代理服务器的地址
7 prop.setProperty("http.proxyHost", HTTP_PROXY_HOST);
8 // 设置http访问要使用的代理服务器的端口
9 prop.setProperty("http.proxyPort", HTTP_PROXY_PORT);
10 // 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
11 prop.setProperty("http.nonProxyHosts", RemoteConfig.PROXT_FILTER_DOMAIN);
12 }
13
14 /**
15 * 移除http代理
16 */
17 public static void removeHttpProxy() {
18 Properties prop = System.getProperties();
19 prop.remove("http.proxyHost");
20 prop.remove("http.proxyPort");
21 prop.remove("http.nonProxyHosts");
22 }
在应用启动时,访问HTTP请求前,设置好就行。当然,http.nonProxyHosts可以不用设置,表示所有的HTTP请求都走代理。
至于HTTPS代理,类似可以这样设置:
System.setProperty("https.proxyHost", "HTTP_PROXY_HOST");
System.setProperty("https.proxyPort", "HTTP_PROXY_PORT");
转载请注明原址:http://www.cnblogs.com/lekko/p/5888962.html
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com