java 获取 IP
String ip = "";
String address = "";
try {
// 获取IP地址
ip = InetAddress.getLocalHost().getHostAddress();
// 获取计算机名
// String hostName = InetAddress.getLocalHost().getHostName(); InetAddress ia = InetAddress.getByName(ip); byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < mac.length; i++) { if (i != 0) { sb.append(“-”); } String hexString = Integer.toHexString(mac[i] & 0xFF); sb.append(hexString.length() == 1 ? “0” + hexString : hexString); } address = sb.toString().toUpperCase(); } catch (Exception e) { e.printStackTrace(); }
vue 获取 IP地址
第一种: // 获取IP地址 const testScript = document.createElement(‘script’) testScript.setAttribute(‘charset’, ‘utf-8’) testScript.setAttribute(‘type’, ‘text/javascript’) testScript.setAttribute(‘src’, ‘https://pv.sohu.com/cityjson?ie=utf-8’) document.getElementsByTagName(‘head’)[0].appendChild(testScript) testScript.onload = function () { console.log(window.returnCitySN[‘cip’], ‘local’) } 第二种: 直接使用 window.returnCitySN console.log(window.returnCitySN)
|