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;
}
}
04-21 19:17