代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java实现获取用户的MAC地址
作者:
东京
/ 发布于
2019/8/7
/
1002
方法一:将本机地址与局域网内其他机器区分开来 /** * 根据IP地址获取mac地址 * @param ipAddress 127.0.0.1 * @return * @throws SocketException * @throws UnknownHostException */ public static String getLocalMac(String ipAddress) throws SocketException, UnknownHostException { // TODO Auto-generated method stub String str = ""; String macAddress = ""; final String LOOPBACK_ADDRESS = "127.0.0.1"; // 如果为127.0.0.1,则获取本地MAC地址。 if (LOOPBACK_ADDRESS.equals(ipAddress)) { InetAddress inetAddress = InetAddress.getLocalHost(); // 貌似此方法需要JDK1.6。 byte[] mac = NetworkInterface.getByInetAddress(inetAddress) .getHardwareAddress(); // 下面代码是把mac地址拼装成String StringBuilder sb = new StringBuilder(); 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地址并返回 macAddress = sb.toString().trim().toUpperCase(); return macAddress; } else { // 获取非本地IP的MAC地址 try { System.out.println(ipAddress); Process p = Runtime.getRuntime() .exec("nbtstat -A " + ipAddress); System.out.println("===process=="+p); InputStreamReader ir = new InputStreamReader(p.getInputStream()); BufferedReader br = new BufferedReader(ir); while ((str = br.readLine()) != null) { if(str.indexOf("MAC")>1){ macAddress = str.substring(str.indexOf("MAC")+9, str.length()); macAddress = macAddress.trim(); System.out.println("macAddress:" + macAddress); break; } } p.destroy(); br.close(); ir.close(); } catch (IOException ex) { } return macAddress; } } 方法二: package com.alpha.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; public class GetMac { /** * java获取客户端网卡的MAC地址 * * @param args */ public static void main(String[] args) { GetMac get = new GetMac(); System.out.println("1="+get.getMAC()); System.out.println("2="+get.getMAC("127.0.0.1")); } // 1.获取客户端ip地址( 这个必须从客户端传到后台): // jsp页面下,很简单,request.getRemoteAddr() ; // 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换 // public String getMyIP() { // try { // FacesContext fc = FacesContext.getCurrentInstance(); // HttpServletRequest request = (HttpServletRequest) fc // .getExternalContext().getRequest(); // return request.getRemoteAddr(); // } catch (Exception e) { // e.printStackTrace(); // } // return ""; // } // 2.获取客户端mac地址 // 调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下: // 运行速度【快】 public String getMAC() { String mac = null; try { Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all"); InputStream is = pro.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String message = br.readLine(); int index = -1; while (message != null) { if ((index = message.indexOf("Physical Address")) > 0) { mac = message.substring(index + 36).trim(); break; } message = br.readLine(); } System.out.println(mac); br.close(); pro.destroy(); } catch (IOException e) { System.out.println("Can't get mac address!"); return null; } return mac; } // 运行速度【慢】 public String getMAC(String ip) { String str = null; String macAddress = null; try { Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (; true;) { str = input.readLine(); if (str != null) { if (str.indexOf("MAC Address") > 1) { macAddress = str .substring(str.indexOf("MAC Address") + 14); break; } } } } catch (IOException e) { e.printStackTrace(System.out); return null; } return macAddress; } } 方法三,更精简一些: import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; public class MACAddress { String ip; String mac; public MACAddress (String ip){ this.ip = ip; } public MACAddress (){ this.ip = "0.0.0.0"; } public String getMac(){ try { Process p = Runtime.getRuntime().exec("arp -n"); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); p.waitFor(); boolean flag = true; String ipStr = "(" + this.ip + ")"; while(flag) { String str = input.readLine(); if (str != null) { if (str.indexOf(ipStr) > 1) { int temp = str.indexOf("at"); this.mac = str.substring( temp + 3, temp + 20); break; } } else flag = false; } } catch (IOException | InterruptedException e) { e.printStackTrace(System.out); } return this.mac; } public void setIp(String ip){ this.ip = ip; } } java通过ip获取mac地址-封ip封mac地址 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 获取MAC地址 * @author * 2011-12 */ public class GetMacAddress { public static String callCmd(String[] cmd) { String result = ""; String line = ""; try { Process proc = Runtime.getRuntime().exec(cmd); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null) { result += line; } } catch(Exception e) { e.printStackTrace(); } return result; } /** * * @param cmd 第一个命令 * @param another 第二个命令 * @return 第二个命令的执行结果 */ public static String callCmd(String[] cmd,String[] another) { String result = ""; String line = ""; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); proc.waitFor(); //已经执行完第一个命令,准备执行第二个命令 proc = rt.exec(another); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null) { result += line; } } catch(Exception e) { e.printStackTrace(); } return result; } /** * * @param ip 目标ip,一般在局域网内 * @param sourceString 命令处理的结果字符串 * @param macSeparator mac分隔符号 * @return mac地址,用上面的分隔符号表示 */ public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) { String result = ""; String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})"; Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(sourceString); while(matcher.find()){ result = matcher.group(1); if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) { break; //如果有多个IP,只匹配本IP对应的Mac. } } return result; } /** * * @param ip 目标ip * @return Mac Address * */ public static String getMacInWindows(final String ip){ String result = ""; String[] cmd = { "cmd", "/c", "ping " + ip }; String[] another = { "cmd", "/c", "arp -a" }; String cmdResult = callCmd(cmd,another); result = filterMacAddress(ip,cmdResult,"-"); return result; } /** * @param ip 目标ip * @return Mac Address * */ public static String getMacInLinux(final String ip){ String result = ""; String[] cmd = { "/bin/sh", "-c", "ping " + ip + " -c 2 && arp -a" }; String cmdResult = callCmd(cmd); result = filterMacAddress(ip,cmdResult,":"); return result; } /** * 获取MAC地址 * @return 返回MAC地址 */ public static String getMacAddress(String ip){ String macAddress = ""; macAddress = getMacInWindows(ip).trim(); if(macAddress==null||"".equals(macAddress)){ macAddress = getMacInLinux(ip).trim(); } return macAddress; } //做个测试 public static void main(String[] args) { System.out.println(getMacAddress("220.181.111.148")); } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
东京
贡献的其它代码
(
11
)
.
java实现获取用户的MAC地址
.
实现两个文本框同时输入
.
HTML5页面直接调用百度地图API,获取当前位置,直接导
.
连接四个数据表
.
控制TD字体颜色
.
div+css 百分比进度条
.
GZIP .gz 文件解压与压缩工具类
.
日期分割,做按时间统计时会用到
.
触屏版类似刷新页面文本框获取焦点的同时弹出手机键盘
.
查找ID工具类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3