集册 Java实例教程 使用InetAddress获取IP地址

使用InetAddress获取IP地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

464
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用InetAddress获取IP地址

/*来自 
 时 代 J a v a - nowjava.com*/

//package com.nowjava;


import java.net.InetAddress;

import java.net.UnknownHostException;


public class Main {

    public static void main(String[] argv) throws Exception {

        System.out.println(getIPAddress());

    }


    public static final String IPADDRESS = "0.0.0.0";


    public static String getIPAddress() {

        String ip = IPADDRESS;/*n o w  j a v a  . c o m*/


        try {

            String ha = InetAddress.getLocalHost().getHostAddress();

            InetAddress[] a = InetAddress.getAllByName(ha);


            if (a.length == 1) {
展开阅读全文