java获取本地Ip地址

package liu.water.java_exec_3;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
/**
 * 
 * ClassName: GetLocalIp
 * @Description: 获取本地IP
 * @author liusz
 * @date 2014-9-5
 */
public class GetLocalIp {
    public static void main(String[] args) {
        try {
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                List<InterfaceAddress> addresses = networkInterface.getInterfaceAddresses();
                for (InterfaceAddress interfaceAddress : addresses) {
                    String ip = interfaceAddress.getAddress().getHostAddress();
                    if (ip.length() < 20) {
                        System.out.println("你的本地IP为:" + ip);
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}


你可能感兴趣的:(java获取本地Ip地址)