当前位置: 技术问答>java相关
请问Java如何得到本地机器名字?
来源: 互联网 发布时间:2015-07-30
本文导语: 请问Java如何得到本地机器名字? | String hostname = InetAddress.getLocalHost().getHostName(); | import java.net.*; public class NetTool{ InetAddress myIPaddress=null; public static void main(String args[]...
请问Java如何得到本地机器名字?
|
String hostname = InetAddress.getLocalHost().getHostName();
|
import java.net.*;
public class NetTool{
InetAddress myIPaddress=null;
public static void main(String args[]){
NetTool mytool;
mytool=new NetTool();
InetAddress tempIpAddress=mytool.getMyIP();
//get your local hostname
String localName=tempIpAddress.getHostName();
}
public InetAddress getMyIP(){
try {
myIPaddress=InetAddress.getLocalHost();
}
catch(UnknownHostException e){}
return myIPaddress;
}
}
public class NetTool{
InetAddress myIPaddress=null;
public static void main(String args[]){
NetTool mytool;
mytool=new NetTool();
InetAddress tempIpAddress=mytool.getMyIP();
//get your local hostname
String localName=tempIpAddress.getHostName();
}
public InetAddress getMyIP(){
try {
myIPaddress=InetAddress.getLocalHost();
}
catch(UnknownHostException e){}
return myIPaddress;
}
}
|
import javax.servlet.*;包中有一个ServletRequest或ServletResponse里面也有一个方法可以得到主机名或端口等