当前位置:酷百书>百科问答>java如何获取主机名字

java如何获取主机名字

2024-06-17 17:10:49 编辑:join 浏览量:609

java如何获取主机名字

java如何获取主机名字

request.getServerName();//网页版

System.getProperty("os.name"); //javaEE版

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Demo {

public static void main(String[] args) {

InetAddress a;

try {

a = InetAddress.getLocalHost();

System.out.println("主机名称: " + a.getHostName());

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

我已经测试过了...

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Demo {

public static void main(String[] args) {

InetAddress a;

try {

a = InetAddress.getLocalHost();

System.out.println("主机名称: " + a.getHostName());

} catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

用InetAddress取得

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Test {

static public void main(String 参数[]){

try {

InetAddress a =InetAddress.getLocalHost();

System.out.println("主机名称: " + a.getHostName());

}

catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

InetAddress add= InetAddress.getByName(ip);

String hostname=add.getHostName();

标签:java,主机,获取

版权声明:文章由 酷百书 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.kubaishu.com/answer/436496.html
热门文章