集册 Java实例教程 从主机获取计算机名称

从主机获取计算机名称

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

455
从主机获取计算机名称


import java.io.UnsupportedEncodingException;/**来 自 N  o w  J a v a . c o m**/

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.ArrayList;


public class Main{

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

        System.out.println(getComputerName());

    }

    

    public static final String IPADDRESS = "0.0.0.0";

    public static String getComputerName() {


        String hostname = null;


        try {

            hostname = InetAddress.getLocalHost().getHostName();

        } catch (java.net.UnknownHostException uhe) {
        /*
         from 时 代 J a v a - nowjava.com 
        */


        }

        return hostname;

    }

    

    public static String getHostName() {


        String hostname = null;


        try {

            hostname = InetAddress.getLocalHost().getHostName();

        } catch (java.net.UnknownHostException uhe) {


        }

        //hostname = "testmachine";

        return hostname + "[@" + AddressUtil.getIPAddress() + "]";

    }

    

    public static String getIPAddress() {

        String ip = IPADDRESS;


        try {

            String ha = InetAddress.
展开阅读全文