In a project, if you want to obtain the IP address of the system, many people may think about how easy it is, but it is not as easy as it seems to be in multiple operating systems, the following shows how to obtain the IP address of the system.
Java code
Package easyway. tbs. commons;
Import java.net. InetAddress;
Import java.net. NetworkInterface;
Import java.net. SocketException;
Import java.net. UnknownHostException;
Import java. util. Enumeration;
Import java. util. Properties;
Import org. apache. log4j. Logger;
/**
*
* Local system information
* @ Author longgangbai
*
*/
Public final class SystemHelper {
Private static final Logger logger = Logger. getLogger (SystemHelper. class );
// Obtain the system property set
Public static Properties props = System. getProperties ();
// Operating system name
Public static String OS _NAME = getPropertery ("OS. name ");
// Line paging character
Public static String OS _LINE_SEPARATOR = getPropertery ("line. separator ");
// File Separator
Public static String OS _FILE_SEPARATOR = getPropertery ("file. separator ");
/**
*
* Obtain the IP address of the server based on the system type.
*
* InetAddress inet = InetAddress. getLocalHost ();
* However, the above Code returns 127.0.0.1 in Linux.
* In linux, the IP address of the localhost configured in/etc/hosts is returned,
* Instead of the NIC binding address. Later, you can use the bound IP address of the NIC to obtain the local IP Address :):
* @ Throws UnknownHostException
*/
Public static InetAddress getSystemLocalIp () throws UnknownHostException {
InetAddress inet = null;
String osname = getSystemOSName ();
Try {
// For The window System
If (osname. inclusignorecase ("Windows XP ")){
Inet = getWinLocalIp ();
// For linux
} Else if (osname. inclusignorecase ("Linux ")){
Inet = getUnixLocalIp ();
}
If (null = inet ){
Throw new UnknownHostException ("unknown host IP Address ");
}
} Catch (SocketException e ){
Logger. error ("getting the local ip address error" + e. getMessage ());
Throw new UnknownHostException ("Get local ip error" + e. getMessage ());
}
Return inet;
}
/**
* Obtain the FTP configuration Operating System
* @ Return
*/
Public static String getSystemOSName (){
// Obtain the system property set
Properties props = System. getProperties ();
// Operating system name
String osname = props. getProperty ("OS. name ");
If (logger. isDebugEnabled ()){
Logger.info ("the ftp client system OS Name" + osname );
}
Return osname;
}
/**
* Get the attribute value
* @ Param propertyName
* @ Return
*/
Public static String getPropertery (String propertyName ){
Return props. getProperty (propertyName );
}
/**
* Obtain the local IP address of the window.
* @ Return
* @ Throws UnknownHostException
*/
Private static InetAddress getWinLocalIp () throws UnknownHostException {
InetAddress inet = InetAddress. getLocalHost ();
System. out. println ("local ip =" + inet. getHostAddress ());
Return inet;
}
/**
*
* Multiple IP addresses may obtain only one IP address.
* Obtain the Linux local IP Address
* @ Return
* @ Throws SocketException
*/
Private static InetAddress getUnixLocalIp () throws SocketException {
Enumeration <NetworkInterface> netInterfaces = NetworkInterface. getNetworkInterfaces ();
InetAddress ip = null;
While (netInterfaces. hasMoreElements ())
{
NetworkInterface ni = (NetworkInterface) netInterfaces. nextElement ();
Ip = (InetAddress) ni. getInetAddresses (). nextElement ();
If (! Ip. isSiteLocalAddress ()
&&! Ip. isLoopbackAddress ()
& Ip. getHostAddress (). indexOf (":") =-1)
{
Return ip;
}
Else
{
Ip = null;
}
}
Return null;
}
/**
*
* Get the memory information of the current running program
* @ Return
*/
Public static final String getRAMinfo (){
Runtime rt = Runtime. getRuntime ();
Return "RAM:" + rt. totalMemory () + "bytes total," + rt. freeMemory () + "bytes free .";
}
}