- /**
- * 返回当前进程的pid
- * 共16位
- * */
- public short getPid(){
- return Short.parseShort(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
- }
- /**
- * 返回本机上的mac地址
- * 当存在多个mac地址时,采取含有eth字符串的第一个mac地址
- * 当不存在含有eth标记的mac字符串时,采用获取到的最后一个mac地址
- * mac一共是48bit 即6个byte
- * */
- public byte[] getMac() throws SocketException {
- byte[] mac=null;
- Enumeration<NetworkInterface> es= NetworkInterface.getNetworkInterfaces();
- while(es.hasMoreElements()){
- NetworkInterface n=es.nextElement();
- if(null!=n.getHardwareAddress()){
- mac=n.getHardwareAddress();
- }
- if(n.getDisplayName().contains("eth")){
- break;
- }
- }
- return mac;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0211201513931.html
来源: http://www.codesnippet.cn/detail/0211201513931.html