【Java】得到本機IP

Dsp Tian發表於2017-09-02
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class MainProcess {
    public static void main(String[] args) {
        InetAddress localhost = null;
        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println ("localhost: "+localhost.getHostAddress());
        System.out.println ("localhost: "+localhost.getHostName());
    }
}

 

相關文章