MAC地址进行验证的方法
需要对对应的MAC地址进行验证的方法,以为很简单就能过,鼓捣了半天以后才发现,我的机器是window7,查询出来是乱码,居然不给支持。没办法在网上继续找资料。终于找到了,贴上来,以备不时之需。
东西都有注释,自己看吧,
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- /**
- * 与系统相关的一些常用工具方法.
- *
- * @author lvbogun
- * @version 1.0.0
- */
- public class SystemTool {
- /**
- * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
- */
- public static String getOSName() {
- return System.getProperty("os.name").toLowerCase();
- }
- /**
- * 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取.
- * 如果有特殊系统请继续扩充新的取mac地址方法.
- *
- * @return mac地址
- */
- public static String getUnixMACAddress() {
- String mac = null;
- BufferedReader bufferedReader = null;
- Process process = null;
- try {
- // linux下的命令,一般取eth0作为本地主网卡
- process = Runtime.getRuntime().exec("ifconfig eth0");
- // 显示信息中包含有mac地址信息
- bufferedReader = new BufferedReader(new InputStreamReader(
- process.getInputStream()));
- String line = null;
- int index = -1;
- while ((line = bufferedReader.readLine()) != null) {
- // 寻找标示字符串[hwaddr]
- index = line.toLowerCase().indexOf("hwaddr");
- if (index >= 0) {// 找到了
- // 取出mac地址并去除2边空格
- mac = line.substring(index + "hwaddr".length() + 1).trim();
- break;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (bufferedReader != null) {
- bufferedReader.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- bufferedReader = null;
- process = null;
- }
- return mac;
- }
- /**
- * 获取widnows网卡的mac地址.
- *
- * @return mac地址
- */
- public static String getWindowsMACAddress() {
- String mac = null;
- BufferedReader bufferedReader = null;
- Process process = null;
- try {
- // windows下的命令,显示信息中包含有mac地址信息
- process = Runtime.getRuntime().exec("ipconfig /all");
- bufferedReader = new BufferedReader(new InputStreamReader(
- process.getInputStream()));
- String line = null;
- int index = -1;
- while ((line = bufferedReader.readLine()) != null) {
- System.out.println(line);
- // 寻找标示字符串[physical
- index = line.toLowerCase().indexOf("physical address");
- if (index >= 0) {// 找到了
- index = line.indexOf(":");// 寻找":"的位置
- if (index >= 0) {
- System.out.println(mac);
- // 取出mac地址并去除2边空格
- mac = line.substring(index + 1).trim();
- }
- break;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (bufferedReader != null) {
- bufferedReader.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- bufferedReader = null;
- process = null;
- }
- return mac;
- }
- /**
- * windows 7 专用 获取MAC地址
- *
- * @return
- * @throws Exception
- */
- public static String getMACAddress() throws Exception {
- // 获取本地IP对象
- InetAddress ia = InetAddress.getLocalHost();
- // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
- byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
- // 下面代码是把mac地址拼装成String
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < mac.length; i++) {
- if (i != 0) {
- sb.append("-");
- }
- // mac[i] & 0xFF 是为了把byte转化为正整数
- String s = Integer.toHexString(mac[i] & 0xFF);
- sb.append(s.length() == 1 ? 0 + s : s);
- }
- // 把字符串所有小写字母改为大写成为正规的mac地址并返回
- return sb.toString().toUpperCase();
- }
- /**
- * 测试用的main方法.
- *
- * @param argc 运行参数.
- * @throws Exception
- */
- public static void main(String[] argc) throws Exception {
- String os = getOSName();
- System.out.println(os);
- if (os.equals("windows 7")) {
- String mac = getMACAddress();
- System.out.println(mac);
- } else if (os.startsWith("windows")) {
- // 本地是windows
- String mac = getWindowsMACAddress();
- System.out.println(mac);
- } else {
- // 本地是非windows系统 一般就是unix
- String mac = getUnixMACAddress();
- System.out.println(mac);
- }
- }
- }
找了很多都是只支持window和linux的。windows7 因为是中文,不能用同一个方法获取。只能另想办法了
MAC地址进行验证的方法相关推荐
- mac地址随机变化的解决方法(安卓手机通用)不用ROOT
mac地址随机变化的解决方法(安卓手机通用)不用ROOT 首先关闭wifi 1.进工程模式 *#*#3646633#*#* 2.点击 connectivity 3.点击 wifi 4.点击 nvram ...
- Java 跨平台获取 MAC 地址的两种方法
前言 有时我们因为软件授权或者其它需要获取主机唯一标识而需要获取用户主机的MAC地址,而本文则将介绍如何通过Java来实现跨平台获取MAC地址的两种方法,同时具体的代码也已上传到GitHub. 如果不 ...
- 中兴网络设备交换机路由器查看MAC地址表项命令方法
描述:中兴网络设备交换机路由器查看MAC地址表项命令方法 命令: show mac
- 获取MAC地址的四种方法(转)
https://www.cnblogs.com/zlshmily/p/10058560.html zlshmily 在实际的应用系统中,我们往往会需要在程序运行时获取当前机器的网卡的MAC地址,以便作 ...
- 【转载】取得系统中网卡MAC地址的三种方法
From:http://blog.csdn.net/zhangting1987/article/details/2732135 网卡地址这个概念有点混淆不清.因为实际上有两个地址,mac地址和物理地址 ...
- android 取mac地址,Android手机获取Mac地址的几种方法,androidmac
Android手机获取Mac地址的几种方法,androidmac 最常用的方法,通过WiFiManager获取: /** * 通过WiFiManager获取mac地址 * @param context ...
- 组播ip mac转换 c语言,组播IP地址与组播MAC地址之间的换算方法与例子
组播IP地址与组播MAC地址之间的换算方法与例子 1个oui 有2 的24方个MAC地址 组播地址 :2的28次方,224.0.0.0-239.255.255.255 可用组播范围:0100.5E00 ...
- 组播IP地址与组播MAC地址之间的换算方法
MAC地址介绍 络接口卡硬件地址(MAC)是在数据链路层由标准组织IEEE统一定义的. 该定义类型大体上分为: EtherNET 802.3 , Token Ring 802.5 , Token Bu ...
- RealARM 210开发板 u-boot传递mac地址到内核的方法
如果您身边有一款Real210的开发板,而且想修改系统启动后的mac地址.那么本记录提供两种方法. 第一种方法:直接修改mac的配置值 首先找到文件mach-smdkv210.c位置在/arch/ar ...
最新文章
- 为什么mac运行变慢,如何提升mac运行速度
- ajax servlet增删改查,Servlet ajax 文件上传和JDBC+Servler用户表增删改查
- python对角线图_python对角线图_python – 在Seaborn Jointplot上绘制对角线(相等的线)...
- 解决pandas不计算/不保存运算结果/数值不变的问题
- Titlemizer 随机相关的标题生成器插件
- 返回固定页面的web服务器
- 这就是程序员被大厂偏爱的实力!
- swift:创建滚动视图的图片轮播器
- el表达式判断不为空_Java学习72天---EL和JSTL表达式学完.
- redis分布式锁的复杂实现
- java 动态修改prooper_java中OOP自己总结的知识点
- STM32F1开发指南笔记46----字库原理及汉字库创建
- 运放专题:电压比较器
- FDDB人脸数据集dataset的dataset数据集的制作
- 大规模sip信令存储、查询和实时跟踪的实现
- Solidworks 3D Onshape 3D 繪圖自學分享
- gensim安装教程
- java 类型转换
- 苹果开发者账号/AppleID如何更改绑定的手机号
- 单招计算机面试考什么,单招考试笔试考什么?面试考什么?