首先我从PC客户端获得MAC地址,像这样
public void getterMacAddress(){
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
macAddress = sb.toString();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
}
我得到MAC地址成功,但我怎么能发送那个MAC地址到服务器?
您可以将字符串作为URL参数传递给服务器(JSF)
myserver:港口/myproject/innerpage/mac.jsf吗?mac = 5 c - ac - 4 c - 75 - 44 - 4 -
public class Bean {
@ManagedProperty(value="#{param.mac}")
private String mac;
@PostConstruct
public void init() {
System.out.println(mac); // 5C-AC-4C-75-44-4A
}
// ...
}
也看看这个问题:URL jsf2
中的参数我不建议将JSF用于此目的。Servlet或REST web服务——是一个更好的方法。