集册 Java实例教程 获取本地主机IP

获取本地主机IP

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

519
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取本地主机IP
/** nowjava - 时代Java 提 供 **/

//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(getLocalHostIP());

    }


    public static String getLocalHostIP() {

        try {

            InetAddress addr = InetAddress.getLocalHost();

            return addr.getHostAddress();

        } catch (UnknownHostException e) {

            
展开阅读全文