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,主机,获取