package com.cloudssaas.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /*********************************************************************************
* //* Copyright (C) 2014 ××××××××××. All Rights Reserved. //* //* Filename:
* ComputerInfo.java //* Revision: 1.0 //* Author: <yao xiucai> //* Created On:
* 2014年5月21日 //* Modified by: //* Modified On: //* //* Description:
*
* <取网卡物理地址--
* 1.在Windows,Linux系统下均可用;
* 2.通过ipconifg,ifconfig获得计算机信息;
* 3.再用模式匹配方式查找MAC地址,与操作系统的语言无关>
*
* //* Description: <取计算机名--从环境变量中取>
* abstract 限制继承/创建实例
*/
/********************************************************************************/ public abstract class ComputerInfo { private static String macAddressStr = null;
private static String computerName = System.getenv().get("COMPUTERNAME"); private static final String[] windowsCommand = { "ipconfig", "/all" };
private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",
Pattern.CASE_INSENSITIVE); /**
* 获取多个网卡地址
*
* @return
* @throws IOException
*/
private final static List<String> getMacAddressList() throws IOException {
final ArrayList<String> macAddressList = new ArrayList<String>();
final String os = System.getProperty("os.name");
final String command[]; if (os.startsWith("Windows")) {
command = windowsCommand;
} else if (os.startsWith("Linux")) {
command = linuxCommand;
} else {
throw new IOException("Unknow operating system:" + os);
}
// 执行命令
final Process process = Runtime.getRuntime().exec(command); BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
for (String line = null; (line = bufReader.readLine()) != null;) {
Matcher matcher = macPattern.matcher(line);
if (matcher.matches()) {
macAddressList.add(matcher.group(1));
// macAddressList.add(matcher.group(1).replaceAll("[-:]",
// ""));//去掉MAC中的“-”
}
} process.destroy();
bufReader.close();
return macAddressList;
} /**
* 获取一个网卡地址(多个网卡时从中获取一个)
*
* @return
*/
public static String getMacAddress() {
if (macAddressStr == null || macAddressStr.equals("")) {
StringBuffer sb = new StringBuffer(); // 存放多个网卡地址用,目前只取一个非0000000000E0隧道的值
try {
List<String> macList = getMacAddressList();
for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {
String amac = iter.next();
if (!amac.equals("0000000000E0")) {
sb.append(amac);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} macAddressStr = sb.toString(); } return macAddressStr;
} /**
* 获取电脑名
*
* @return
*/
public static String getComputerName() {
if (computerName == null || computerName.equals("")) {
computerName = System.getenv().get("COMPUTERNAME");
}
return computerName;
} /**
* 获取客户端IP地址
*
* @return
*/
public static String getIpAddrAndName() throws IOException {
return InetAddress.getLocalHost().toString();
} /**
* 获取客户端IP地址
*
* @return
*/
public static String getIpAddr() throws IOException {
return InetAddress.getLocalHost().getHostAddress().toString();
} /**
* 获取电脑唯一标识
*
* @return
*/
public static String getComputerID() {
String id = getMacAddress();
if (id == null || id.equals("")) {
try {
id = getIpAddrAndName();
} catch (IOException e) {
e.printStackTrace();
}
}
return computerName;
} /**
* 限制创建实例
*/
private ComputerInfo() { } public static void main(String[] args) throws IOException {
System.out.println(ComputerInfo.getMacAddress());
System.out.println(ComputerInfo.getComputerName());
System.out.println(ComputerInfo.getIpAddr());
System.out.println(ComputerInfo.getIpAddrAndName());
}
}

相关:

java中得到计算机MAC网卡标识,IP,计算机名称等唯一标识问题

java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名的更多相关文章

  1. 获取客户端网卡MAC地址和IP地址实现JS代码

    获取客户端网卡MAC地址和IP地址实现JS代码 作者: 字体:[增加 减小] 类型:转载   获取客户端的一些信息,如IP和MAC,以结合身份验证,相信很多人都会这样做吧,我们这里用Javascrip ...

  2. js获取本机mac地址,IP地址,计算机名

    <!DOCTYPE HTML> <html> <head> <title>js获取本机mac地址,IP地址,计算机名</title> < ...

  3. 获取客户机MAC地址 根据IP地址 获取机器的MAC地址 / 获取真实Ip地址

    [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref ...

  4. c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息

    我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统名称.物理内存等. 首先需要引入命名空间: us ...

  5. 获取设备的mac地址和IP地址(android6.0以上专用)

    /** * 获取设备HardwareAddress地址 * @return */public static String getMachineHardwareAddress(){ Enumeratio ...

  6. iphone开发之获取网卡的MAC地址和IP地址

    本文转载至 http://blog.csdn.net/arthurchenjs/article/details/6358489 这是获取网卡的硬件地址的代码,如果无法编译通过,记得把下面的这几个头文件 ...

  7. Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP

    Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP IP 工具类 源代码: /** * <p> * * @author XiaoPengwei ...

  8. Java工具类——数学相关的类

    Java工具类--数学相关的类 在上一篇文章中,我们系统学习了 Java 里面的包装类,那么这篇文章,我们就来学习一下Java提供好的类--数学相关的类. 一.数学类介绍 在最早期学习 Java 基础 ...

  9. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

随机推荐

  1. centos7 Firewalld操作集合

    =============================================== 2019/4/15_第1次修改                       ccb_warlock == ...

  2. composer 安装依赖缓慢,查看 composer 的详细执行日志

    在 windows WSL 上安装 composer 依赖 composer install 发现执行异常缓慢,怀疑没有走国内的镜像,而是直接访问的 github. 需要能看到 composer 的执 ...

  3. js事件驱动函数

    输入框 获得光标的这个行为叫做获取焦点 失去光标的这个行为叫做失去焦点 blur 失去焦点 1.获取标签的时候,一定要先等页面加载完成,再去获取这个标签. 可以将整个script代码写在body的下面 ...

  4. k8s单机部署1.11.5

    一.概述 由于服务器有限,因此只能用虚拟机搭建 k8s.但是开3个节点,电脑卡的不行. k8s中文社区封装了一个 Minikube,用来搭建单机版,链接如下: https://yq.aliyun.co ...

  5. Linux学习笔记:ftp中binary二进制与ascii传输模式的区别

    在使用ftp传输文件时,常添加上一句: binary  -- 使用二进制模式传输文件 遂查资料,如下所获. FTP可用多种格式传输文件,通常由系统决定,大多数Linux/UNIX系统只有两种模式:文本 ...

  6. Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)

    今天在对 Ubuntu 进行更新源的时候,突然出现下列错误(为了省事,更新前直接切换了 root 用户) 上网查了一下,网上解释说应该是之前那个更新被强制取消的问题,进程仍然还在.用这个命令查看一下: ...

  7. Collections.sort 给集合排序

    List<MenuVo> child_menus = new ArrayList<MenuVo>(); for (MenuVo menuVo : child_menus) { ...

  8. JavaWeb 之 清理错误的无奈之举

    1. Project -> Clean 2. 清空浏览器缓存 (得先按了F2,出了调试窗口,才可以右键它) 3. 右键项目 -> Maven -> Update -> Forc ...

  9. Unity报错 : BCE0004: Ambiguous reference 'preview': CameraMotionBlurEditor.preview, UnityEditor.Editor.preview.

    建立项目版本为Unity4.6,改为5.3.4版本,运行项目报如下错误: “BCE0004: Ambiguous reference 'preview': CameraMotionBlurEditor ...

  10. JVM GC-----3、垃圾标记算法(二)

    在上一篇文章中,介绍了在GC机制中,GC是以什么标准判定对象可以被标记的,以及最有效最常用的可达性分析法.今天介绍另外一种非常常用的标记算法,它的应用面也相当广泛.这就是:引用计数法 Referenc ...