`
dove19900520
  • 浏览: 593029 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java获取本机所有网卡的IP地址(ipv4)

    博客分类:
  • java
阅读更多
/**
	 * 获取机器所有网卡的IP(ipv4)
	 * @return
	 */
	public static List<String> getLocalIP() {
		List<String> ipList = new ArrayList<String>();
		InetAddress ip = null;
		try {
			Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
			while (netInterfaces.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
				// 遍历所有ip
				Enumeration<InetAddress> ips = ni.getInetAddresses();
				while (ips.hasMoreElements()) {
					ip = (InetAddress) ips.nextElement();
					if (null == ip || "".equals(ip)) {
						continue;
					}
					String sIP = ip.getHostAddress();
					if(sIP == null || sIP.indexOf(":") > -1) {
						continue;
					}
					ipList.add(sIP);
					System.out.println(sIP);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ipList;
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics