在Java中,可以使用InetAddress.getLocalHost()的方式来获取本地的IP和Server名字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.devnp;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPTest {

public static void main(String[] args) {

try {
InetAddress ip = InetAddress.getLocalHost() ;

System.out.println("Host Name : " + ip.getHostName() + " Host IP : " + ip.getHostAddress());

} catch (UnknownHostException e) {
e.printStackTrace();
}

}
}

结果:
Host Name : duliu Host IP : 192.168.56.1