java 获取本机ip mac_Java获取本机IP地址和MAC地址

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.net.UnknownHostException;

import java.util.Date;

import java.util.concurrent.ThreadLocalRandom;

public class Test {

public static void main(String[] args) {

InetAddress inet;

try {

inet = InetAddress.getLocalHost();

System.out.println(inet);

System.out.println(inet.getHostName()); // 名称

System.out.println(inet.getHostAddress()); // 只获得IP

byte[] mac = NetworkInterface.getByInetAddress(inet).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地址并返回

String macAddress = sb.toString().trim().toUpperCase();

System.out.println(macAddress); // 只获得IP

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SocketException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

getMAC();

}

public static 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, "GBK"));

String message = br.readLine();

int index = -1;

while (message != null) {

if ((index = message.indexOf("物理地址")) > 0) {

mac = message.substring(index + 32).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;

}

private static String decodeSpecialWord(String word) {

String result = "";

result = word.replace("&", "\\u0026");

result = result.replace("

result = result.replace(">", "\\u003E");

result = result.replace("\"", "\\u0022");

result = result.replace("'", "\\u0027");

return result;

}

/**

* @param max

* @return

*/

private static int randomData(int max) {

System.out.println("randomData Start" + new Date(System.currentTimeMillis()));

int s = ThreadLocalRandom.current().nextInt(3);

System.out.println(s + " randomData ENd" + new Date(System.currentTimeMillis()));

return s;

}

/**

*

* @param bString

* @return 转换为16进制字符串

*/

private static String binaryString2hexString(String bString) {

if (bString == null || bString.equals("") || bString.length() % 8 != 0)

return null;

StringBuffer tmp = new StringBuffer();

int iTmp = 0;

for (int i = 0; i < bString.length(); i += 4) {

iTmp = 0;

for (int j = 0; j < 4; j++) {

iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);

}

tmp.append(Integer.toHexString(iTmp));

}

return tmp.toString();

}

/**

*

* @param hexString

* @return 转换为二进制字符串

*/

private static String hexString2binaryString(String hexString) {

if (hexString == null || hexString.length() % 2 != 0)

return null;

String bString = "", tmp;

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

tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));

bString += tmp.substring(tmp.length() - 4);

}

return bString;

}

/**

* 10进制转换为16进制字符串

*

* @param time

* @return

*/

private static String getHexString(int time) {

String hexTime = Integer.toHexString(time);

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

hexTime = "0" + hexTime;

}

return hexTime;

}

/**

* 用于在DB[int, 分钟数]view[String HH:MM]间的转换

*

* @param time

* 格式为 "hh:mm"

* @return

*/

private static int getTimeFormat(String time) {

int hour = 0;

int min = 0;

hour = Integer.parseInt(time.substring(0, 2));

min = Integer.parseInt(time.substring(3, 5));

return hour * 6 + min / 10;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值