linux/Windows中JAVA获取IP地址

	public  void getIpAddr() {
		Enumeration allNetInterfaces = null;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
	
			e.printStackTrace();
		}
		InetAddress ip = null;
		while (allNetInterfaces.hasMoreElements()) {
			NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
					.nextElement();
			System.out.println(netInterface.getName());
			Enumeration addresses = netInterface.getInetAddresses();
			while (addresses.hasMoreElements()) {
				ip = (InetAddress) addresses.nextElement();
				if (ip != null && ip instanceof Inet4Address
						&& ip.getHostAddress().indexOf(".") != -1) {
					System.out.println("本机的IP = " + ip.getHostAddress());
					logger.debug("------------------本机的IP : "+ip.getHostAddress()+"---------------------");
				}
			}
		}

	}

 
阅读更多
文章标签: java null
个人分类: Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭