Java obtains the system IP Address

Source: Internet
Author: User
Tags file separator

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 .";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.