java獲取本機IP


如果是在windows環境: 使用InetAddress.getLocalHost()方法即可.


   
   
  
  
          
  1. import java.net.InetAddress;
  2. public class Main {
  3. public static void main(String[] args)
  4. throws Exception {
  5. InetAddress addr = InetAddress.getLocalHost();
  6. System.out.println( "Local HostAddress:
  7. "+addr.getHostAddress());
  8. String hostname = addr.getHostName();
  9. System.out.println( "Local host name: "+hostname);
  10. }
  11. }

代碼運行結果:


   
   
  
  
          
  1. Local HostAddress: 192.168.42.2
  2. Local host name: f19ca2b695da

在linux下上述獲取IP的方式有時候會得到127.0.0.1.

從JDK1.4開始,Java提供了一個NetworkInterface類。這個類可以得到本機所有的物理網絡接口和虛擬機等軟件利用本機的物理網絡接口創建的邏輯網絡接口的信息,NetworkInterface可以通過getNetworkInterfaces方法來枚舉本機所有的網絡接口。我們也可以利用getNetworkInterfaces得到的網絡接口來枚舉本機的所有IP地址。當然,也可以通過InetAddress類的getAllByName來得到本機的所有IP地址:

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
  
  
 
 
         

但getNetworkInterfaces方法可以按網絡接口將這些IP地址進行分組,這對於只想得到某個網絡接口上的所有IP地址是非常有用的。NetworkInterface類提供了三個方法可以分別得到網絡接口名(getName方法)、網絡接口別名(getDisplayName方法)以及和網絡接口綁定的所有IP地址(getInetAddresses方法):

1. getName方法
這個方法用來得到一個網絡接口的名稱。這個名稱就是使用getByName 方法創建NetworkInterface 對象時使用的網絡接口名,如eth0 ppp0 等。getName 方法的定義如下:
public String getName()
  
  
 
 
         
2. getDisplayName方法
這個方法可以得到更容易理解的網絡接口名,也可以將這個網絡接口名稱為網絡接口別名。在一些操作系統中(如Unix ),getDisplayName 方法和getName 方法的返回值相同,但在Windows getDisplayName 方法一般會返回一個更為友好的名字,如 Realtek RTL8139 Family PCI Fast Ethernet NIC getDisplayName 方法的定義如下:

public String getDisplayName()
  
  
 
 
         

3. getInetAddresses方法
NetworkInterface 類可以通過getInetAddresse 方法以InetAddress 對象的形式返回和網絡接口綁定的所有IP 地址。getInetAddresses 方法的定義如下:
public Enumeration<InetAddress> getInetAddresses()
  
  
 
 
         

下面給出windows和linux下通用的獲取本機IP的方法:


   
   
  
  
          
  1. import java.net.Inet4Address;
  2. import java.net.InetAddress;
  3. import java.net.NetworkInterface;
  4. import java.util.Enumeration;
  5. public class Main {
  6. public static void main(String[] args) {
  7. System.out.println( "本機IP:" + getIpAddress());
  8. }
  9. public static String getIpAddress() {
  10. try {
  11. Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  12. InetAddress ip = null;
  13. while (allNetInterfaces.hasMoreElements()) {
  14. NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
  15. if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
  16. continue;
  17. } else {
  18. Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  19. while (addresses.hasMoreElements()) {
  20. ip = addresses.nextElement();
  21. if (ip != null && ip instanceof Inet4Address) {
  22. return ip.getHostAddress();
  23. }
  24. }
  25. }
  26. }
  27. } catch (Exception e) {
  28. System.err.println( "IP地址獲取失敗" + e.toString());
  29. }
  30. return "";
  31. }
  32. }

表示對網絡接口進行篩選,非回送接口 且 非虛擬網卡 且 正在使用中

注:

netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp() 用於排除回送接口,非虛擬網卡,未在使用中的網絡接口.


本文參考:

http://www.runoob.com/java/net-localip.html

https://www.oschina.net/question/129471_39474

http://blog.51cto.com/androidguy/214458

原文地址:https://blog.csdn.net/nianbingsihan/article/details/80265029


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM