java 得到本机所有ip

java 得到本机所有ip

 

public   static  String[] getAllLocalHostIP()
    
{
        String[] ret 
= null;
        
try
        
{
            String hostName 
= InetAddress.getLocalHost().getHostName();
            
if (hostName == null || hostName.length() < 1)
            
{
                
return ret;
            }

            InetAddress[] inetAddresses 
= InetAddress.getAllByName(hostName);
            
if (inetAddresses == null || inetAddresses.length < 1)
            
{
                
return ret;
            }

            
            ret 
= new String[inetAddresses.length];
            
for (int i = 0; i < inetAddresses.length; i++)
            
{
                ret[i] 
= inetAddresses[i].getHostAddress();
                
            }

        }

        
catch (Exception ex)
        
{
            ex.printStackTrace();
        }

        
return ret;
    }

你可能感兴趣的:(java 得到本机所有ip)