Here you can find the source of getIpByNetworkInterfaceName(String name)
public static List<String> getIpByNetworkInterfaceName(String name)
//package com.java2s; /**/*from ww w. j a va2s . co m*/ * * @Title NetworkUtils.java * @Description ?AppStatus??????????????? * Copyright: Copyright (c) 2013, Opzoon and/or its affiliates. All rights reserved. * OPZOON PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * @author NY * @date 2013-11-30 ????11:02:07 * */ import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class Main { public static List<String> getIpByNetworkInterfaceName(String name) { List<String> resultList = null; NetworkInterface networkInterface = null; for (int i = 0; i < getUsualNetworkInterfaces().size(); i++) { networkInterface = getUsualNetworkInterfaces().get(i); if (name.equals(networkInterface.getName())) { resultList = getInetAddressList(networkInterface); } } return resultList; } public static List<NetworkInterface> getUsualNetworkInterfaces() { List<NetworkInterface> resultList = new ArrayList<NetworkInterface>(); Enumeration<NetworkInterface> allNetInterfaces; try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) { continue; } resultList.add(netInterface); } } catch (SocketException e) { System.out .println("Some error occured during execute method getServerUsualInterface, see details:" + e); } return resultList; } public static List<String> getInetAddressList(NetworkInterface netInterface) { List<String> inetAddressList = new ArrayList<String>(); Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); InetAddress ip = null; while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); if (ip != null && ip instanceof Inet4Address) { inetAddressList.add(ip.getHostAddress()); } } return inetAddressList; } }