java 獲取本地系統IP 及網咯IP
阿新 • • 發佈:2018-12-22
public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost();//獲取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121 String hostAddress = address.getHostAddress();//192.168.0.121 InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");//獲取的是該網站的ip地址,比如我們所有的請求都通過nginx的,所以這裡獲取到的其實是nginx伺服器的IP地 String hostAddress1 = address1.getHostAddress();//124.237.121.122 InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");//根據主機名返回其可能的所有InetAddress物件 for(InetAddress addr:addresses){ System.out.println(addr);//www.baidu.com/14.215.177.38 //www.baidu.com/14.215.177.37} System.out.println(address+"\t-----"); System.out.println(hostAddress+"\t*****"); System.out.println(hostAddress1+"\t+++++++"); } catch (UnknownHostException e) { e.printStackTrace(); } }