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

解决方案 »

  1.   

    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;
     }
    }
      

  2.   

    我写得太麻烦了,佩服masterz!!!
      

  3.   

    import javax.servlet.*;包中有一个ServletRequest或ServletResponse里面也有一个方法可以得到主机名或端口等