标签:
获取局域网ip和mac(如果电脑没有直接连接外网),否则获取公网ip
通过第三放获取公网ip
package org.twt.zipjar.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Inet4Address; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.NetworkInterface; import java.net.SocketException; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; import java.nio.charset.Charset; import java.util.Enumeration; import java.util.Formatter; import java.util.HashMap; import java.util.Locale; import java.util.Map; public class NetworkUtils { /** * 获取本地IP列表(针对多网卡情况) * @return */ public static Map<String, Object> getLocalInetMac() { Map<String, Object> ipMacInfo = null; try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface .getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces .nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface .getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); ipMacInfo = pickInetAddress(inetAddress, networkInterface); if (ipMacInfo != null) { return ipMacInfo; } } } } catch (SocketException e) { e.printStackTrace(); } return null; } private static Map<String, Object> pickInetAddress(InetAddress inetAddress, NetworkInterface ni) { try { String name = ni.getDisplayName(); if (name.contains("Virtual Ethernet Adapter") || name.contains("Virtual") || name.contains("VMnet")) { return null; } if (ni.isVirtual() || !ni.isUp() || !ni.supportsMulticast()) { return null; } InetAddress localHost = InetAddress.getLocalHost(); if (inetAddress.isSiteLocalAddress() && localHost.getHostName().equals(inetAddress.getHostName())) { Formatter formatter = new Formatter(); String sMAC = null; byte[] macBuf = ni.getHardwareAddress(); for (int i = 0; i < macBuf.length; i++) { sMAC = formatter.format(Locale.getDefault(), "%02X%s", macBuf[i], (i < macBuf.length - 1) ? "-" : "") .toString(); } formatter.close(); Map<String, Object> ipMacInfo = new HashMap<String, Object>(); ipMacInfo.put("hostname", inetAddress.getHostName()); //系统当前hostname ipMacInfo.put("ip", inetAddress.getHostAddress()); //ip地址 ipMacInfo.put("ipnet", inetAddressTypeName(inetAddress)); //网络类型 ipMacInfo.put("os", System.getProperty("os.name")); //系统名称 ipMacInfo.put("mac", sMAC); //mac 地址 ipMacInfo.put("cpu-arch", System.getProperty("os.arch")); //cpu架构 ipMacInfo.put("network-arch", ni.getDisplayName()); //网卡名称 return ipMacInfo; } } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } private static String inetAddressTypeName(InetAddress inetAddress) { return (inetAddress instanceof Inet4Address) ? "ipv4" : "ipv6"; } //通过第三方网站http://1111.ip138.com/ic.asp获取ip private static Map<String, String> getOpenNetworkIp() { try { URLConnection openConnection = new URL("http://1111.ip138.com/ic.asp").openConnection(); openConnection.setDoInput(true); openConnection.connect(); InputStream is = (InputStream) openConnection.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is,Charset.forName("GBK"))); StringBuilder sb = new StringBuilder(); String str = null; while((str=br.readLine())!=null) { sb.append(str); } String htmlSrc = sb.toString().toLowerCase(Locale.getDefault()); String startTag = "<center>"; String endTag = "</center>"; htmlSrc = htmlSrc.substring(htmlSrc.indexOf(startTag)+startTag.length(), htmlSrc.lastIndexOf(endTag)); String openIp = htmlSrc.substring(htmlSrc.indexOf("[")+1, htmlSrc.lastIndexOf("]")); String provider = htmlSrc.substring(htmlSrc.lastIndexOf(":")+1); Map<String, String> resultMap = new HashMap<String, String>(); resultMap.put("openIp", openIp); resultMap.put("provider", provider); br.close(); return resultMap; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { Map<String, Object> localInetMac = getLocalInetMac(); System.out.println(localInetMac); Map<String, String> openNetworkIp = getOpenNetworkIp(); System.out.println(openNetworkIp); } }
如上方案在Android,Windows,linux都成功了,可能有些地方考虑不太周全,但应该覆盖了90-95%的PC和Android系统,对了,貌似不支持Android 2.2,不过Android本身就可获取mac
public String getMacAddress( ) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); String mac_address = (wifiManager == null ? "" : wifiManager.getConnectionInfo().getMacAddress()); mac_address = (mac_address == null ? "" : mac_address); return mac_address; }
下面是运行在Android上的效果
标签:
原文地址:http://my.oschina.net/ososchina/blog/501447