您的位置:

java获取mac,java获取mac很卡

本文目录一览:

java怎么获取用户客户端mac地址

以下代码实现浏览器中获取mac地址放入一个输入框或隐藏域,随登录信息一起提交到服务器。因为安全级别的关系可能会出现警告,可选择允许执行。长期使用的话建议用户将改站点加入信任站点或把安全级别调低。

HTMLHEADTITLEWMI Scripting HTML/TITLE

META http-equiv=Content-Type content="text/html; charset=gb2312"

SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo

document.forms[0].txtMACAddr.value=unescape(MACAddr);

document.forms[0].txtIPAddr.value=unescape(IPAddr);

document.forms[0].txtDNSName.value=unescape(sDNSName);

//document.formbar.submit();

/SCRIPT

SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo

if(objObject.IPEnabled != null objObject.IPEnabled != "undefined" objObject.IPEnabled == true)

{

if(objObject.MACAddress != null objObject.MACAddress != "undefined")

MACAddr = objObject.MACAddress;

if(objObject.IPEnabled objObject.IPAddress(0) != null objObject.IPAddress(0) != "undefined")

IPAddr = objObject.IPAddress(0);

if(objObject.DNSHostName != null objObject.DNSHostName != "undefined")

sDNSName = objObject.DNSHostName;

}

/SCRIPT

META content="MSHTML 6.00.2800.1106" name=GENERATOR/HEAD

BODY

OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT/OBJECT

OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223/OBJECT

SCRIPT language=JScript

var service = locator.ConnectServer();

var MACAddr ;

var IPAddr ;

var DomainAddr;

var sDNSName;

service.Security_.ImpersonationLevel=3;

service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');

/SCRIPT

FORM id="formfoo" name="formbar" action="index.do" method="post"INPUT value="00-11-11-B4-52-EF" name="txtMACAddr" INPUT value="210.42.38.50" name="txtIPAddr" INPUT value="zhupan" name="txtDNSName" /FORM/BODY/HTML

JAVA如何获取客户端IP地址和MAC地址

/**

* 获取IP地址

* @return

*/

public static String GetNetIp() {

URL infoUrl = null;

InputStream inStream = null;

String line = "";

try {

infoUrl = new URL("");

URLConnection connection = infoUrl.openConnection();

HttpURLConnection httpConnection = (HttpURLConnection) connection;

int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

inStream = httpConnection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));

StringBuilder strber = new StringBuilder();

while ((line = reader.readLine()) != null)

strber.append(line + "\n");

inStream.close();

// 从反馈的结果中提取出IP地址

int start = strber.indexOf("{");

int end = strber.indexOf("}");

String json = strber.substring(start, end + 1);

if (json != null) {

try {

JSONObject jsonObject = new JSONObject(json);

line = jsonObject.optString("cip");

} catch (JSONException e) {

e.printStackTrace();

}

}

return line;

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return line;

}

public static String getLocalMacAddress() {//没有缓存的地址,则查询

String mac_s = ""; try { byte[] mac;

NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));

mac = ne.getHardwareAddress();

mac_s = byte2hex(mac);

} catch (Exception e) {

} mac_s; return mac_s;

}

java中怎么获取电脑的mac地址

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.net.UnknownHostException;

/*

 * 物理地址是48位,别和ipv6搞错了

 */

public class LOCALMAC {

/**

 * @param args

 * @throws UnknownHostException 

 * @throws SocketException 

 */

public static void main(String[] args) throws UnknownHostException, SocketException {

// TODO Auto-generated method stub

//得到IP,输出PC-201309011313/122.206.73.83

InetAddress ia = InetAddress.getLocalHost();

System.out.println(ia);

getLocalMac(ia);

}

private static void getLocalMac(InetAddress ia) throws SocketException {

// TODO Auto-generated method stub

//获取网卡,获取地址

byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

System.out.println("mac数组长度:"+mac.length);

StringBuffer sb = new StringBuffer("");

for(int i=0; imac.length; i++) {

if(i!=0) {

sb.append("-");

}

//字节转换为整数

int temp = mac[i]0xff;

String str = Integer.toHexString(temp);

System.out.println("每8位:"+str);

if(str.length()==1) {

sb.append("0"+str);

}else {

sb.append(str);

}

}

System.out.println("本机MAC地址:"+sb.toString().toUpperCase());

}

}

java如何查询本机ip地址和mac地址

// 获取mac地址

  public static String getMacAddress() {

    try {

      EnumerationNetworkInterface allNetInterfaces = NetworkInterface.getNetworkInterfaces();

      byte[] mac = null;

      while (allNetInterfaces.hasMoreElements()) {

        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {

          continue;

        } else {

          mac = netInterface.getHardwareAddress();

          if (mac != null) {

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i  mac.length; i++) {

              sb.append(String.format("%02X%s", mac[i], (i  mac.length - 1) ? "-" : ""));

            }

            if (sb.length()  0) {

              return sb.toString();

            }

          }

        }

      }

    } catch (Exception e) {

      _logger.error("MAC地址获取失败", e);

    }

    return "";

  }

 

  // 获取ip地址

  public static String getIpAddress() {

    try {

      EnumerationNetworkInterface allNetInterfaces = NetworkInterface.getNetworkInterfaces();

      InetAddress ip = null;

      while (allNetInterfaces.hasMoreElements()) {

        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {

          continue;

        } else {

          EnumerationInetAddress addresses = netInterface.getInetAddresses();

          while (addresses.hasMoreElements()) {

            ip = addresses.nextElement();

            if (ip != null  ip instanceof Inet4Address) {

              return ip.getHostAddress();

            }

          }

        }

      }

    } catch (Exception e) {

      _logger.error("IP地址获取失败", e);

    }

    return "";

  }

希望能帮助到你

java如何获取mac地址?

以windows举例。

运行命令" cmd ipconfig /all"就会出现以下结果

Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE

java就能过这样的命令来获取。以下是示例。

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class TestMac

{

public static void main(String[] args) {

System.out.println("Operation System=" + getOsName());

System.out.println("Mac Address=" + getMACAddress());

System.out.println("通过ip获取mac"+getMACAddress("192.168.1.101"));

}

public static String getOsName() {

String os = "";

os = System.getProperty("os.name");

return os;

}

public static String getMACAddress() {

String address = "";

String os = getOsName();

if (os.startsWith("Windows")) {

try {

String command = "cmd.exe /c ipconfig /all";

Process p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

if (line.indexOf("Physical Address") 0) {

int index = line.indexOf(":");

index += 2;

address = line.substring(index);

break;

}

}

br.close();

return address.trim();

} catch (IOException e) {

}

} else if (os.startsWith("Linux")) {

String command = "/bin/sh -c ifconfig -a";

Process p;

try {

p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

if (line.indexOf("HWaddr") 0) {

int index = line.indexOf("HWaddr") + "HWaddr".length();

address = line.substring(index);

break;

}

}

br.close();

} catch (IOException e) {

}

}

address = address.trim();

return address;

}

public static String getMACAddress(String ipAddress) {

String str = "", strMAC = "", macAddress = "";

try {

Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);

InputStreamReader ir = new InputStreamReader(pp.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

for (int i = 1; i 100; i++) {

str = input.readLine();

if (str != null) {

if (str.indexOf("MAC Address") 1) {

strMAC = str.substring(str.indexOf("MAC Address") + 14,

str.length());

break;

}

}

}

} catch (IOException ex) {

return "Can't Get MAC Address!";

}

//

if (strMAC.length() 17) {

return "Error!";

}

macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)

+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)

+ ":" + strMAC.substring(12, 14) + ":"

+ strMAC.substring(15, 17);

//

return macAddress;

}

}

剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过

我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。

Java代码如何获取客户端的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; }