Java获取本地IP地址

import java.net.InetAddress;
import java.net.UnknownHostException; public class IpTest { public static void main(String args[]) throws UnknownHostException { String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println(ip);
}
}

功能是实现了,但是写的不好!

抽取!抽取!

public class Test {

    private static final Logger logger = LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) throws ParseException {
String ipAddress = getNativeIp();
System.out.println(ipAddress);
} //获得本机IP
private static String getNativeIp() {
String ipAddrStr = "";
try {
ServerSocket ss = new ServerSocket(0);
ipAddrStr = ss.getInetAddress().getLocalHost().getHostAddress();
ss.close();
} catch (Exception e) {
logger.error("获取本地ip失败:{}", e.getMessage());
}
return ipAddrStr;
}
}
上一篇:magent——memcached缓存代理服务器


下一篇:浅谈V8引擎中的垃圾回收机制