经测试还有查阅资料 getHostName() 有时候会有获取hostname错误的情况,

用getCanonicalHostName() 可以避免这种情况
This commit is contained in:
Dexter 2020-04-02 15:24:45 +08:00
parent 643b6e9c64
commit a571e1c2a7

View File

@ -22,7 +22,8 @@ public class HostUtils {
try { try {
InetAddress addr = InetAddress.getLocalHost(); InetAddress addr = InetAddress.getLocalHost();
ip = addr.getHostAddress(); ip = addr.getHostAddress();
hostname = addr.getHostName(); // hostname = addr.getHostName();
hostname = addr.getCanonicalHostName();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
log.error("Can't find out address: " + e.getMessage()); log.error("Can't find out address: " + e.getMessage());
ip = "UNKNOWN"; ip = "UNKNOWN";