集册 Java实例教程 获取主机名

获取主机名

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

362
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取主机名


//package com.nowjava;
/* 
 来自 
*N o w  J a v a  .   c o m*/

import java.net.InetAddress;

import java.net.UnknownHostException;


public class Main {

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

        System.out.println(getHostName());

    }


    public static String getHostName() {

        String server = null;
/** N o w J a v a . c o m 提 供 **/

        // Obtain server name of local host

        try {

            final InetAddress localMachine = InetAddress.getLocalHost();

            server = localMachine.getHostName();

        } catch (final UnknownHostException e) {

            server = "Unknown";

        }

        return server;

    }


    public static String getHostName(String ipAddress) {

        String server = null;

        try {

            // Get hostname by textual representation of IP address

            final InetAddress addr = 
展开阅读全文