综合了网上找的代码,整理的,Windows和Linux都可以用。


  1. private static String getHostIp(){
  2. try{
  3. Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  4. while (allNetInterfaces.hasMoreElements()){
  5. NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
  6. Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  7. while (addresses.hasMoreElements()){
  8. InetAddress ip = (InetAddress) addresses.nextElement();
  9. if (ip != null
  10. && ip instanceof Inet4Address
  11. && !ip.isLoopbackAddress() //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
  12. && ip.getHostAddress().indexOf(":")==-1){
  13. System.out.println("本机的IP = " + ip.getHostAddress());
  14. return ip.getHostAddress();
  15. }
  16. }
  17. }
  18. }catch(Exception e){
  19. e.printStackTrace();
  20. }
  21. return null;
  22. }

java获取本机IP地址,非127.0.0.1的更多相关文章

  1. java获取本机IP地址

    转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...

  2. 详谈再论JAVA获取本机IP地址

    首先,你如果搜索“JAVA获取本机IP地址”,基本上搜到的资料全是无用的.比如这篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html实际上的代码在复杂环境下 ...

  3. java获取本机ip地址(写出来的)

    /** * @author 豪弟 * @param request * @return * @throws IOException */ public final static String getI ...

  4. java获取本机IP地址和MAC地址的方法

    // 获取ip地址 public static String getIpAddress() { try { Enumeration<NetworkInterface> allNetInte ...

  5. java 获取本机ip地址

    /** * 取当前系统站点本地地址 linux下 和 window下可用 * * @return */ public static String getLocalIP() { String sIP = ...

  6. java获取本机ip(排除虚拟机等一些ip)最终解,总算找到方法了

    本文参考https://blog.csdn.net/u011809209/article/details/77236602 本文参考https://blog.csdn.net/yinshuomail/ ...

  7. Java 实例 - 获取本机ip地址及主机名

    package guyu.day0824; import java.net.InetAddress; /** * @Author: Fred * @Date: 2020/8/24 09:39 */ p ...

  8. shell中获取本机ip地址

    shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...

  9. Java获取本机MAC地址[转]

    原文地址:https://www.cnblogs.com/hxsyl/p/3422191.html Java获取本机MAC地址   为什么写这个呢?因为前几天看见网上有采用windows命令获取局域网 ...

随机推荐

  1. Android Set与List之间转化

    List list = new ArrayList(set); Set set = new HashSet(list); //但是有一点,转换当中可能要丢失数据,尤其是从list转换到set的时候,因 ...

  2. Altium Designer中原理图和pcb交叉查找

    转自:https://wenku.baidu.com/view/53ca06badbef5ef7ba0d4a7302768e9951e76e33.html 再看看:::: 转自:http://blog ...

  3. pdf.js安装步骤和使用

    从github下载的源码不能直接使用,最好使用命令行下载安装 1.下载源码 git clone git://github.com/mozilla/pdf.js.git cd pdf.js 2.安装no ...

  4. jemter--录制的脚本设置循环次数不起作用

    以下是比较jmeter线程组中设置循环次数和循环控制器中设置循环次数的区别 1.jmeter生成的脚本没有step1(循环控制器)控制器,故循环在线程组中设置   2.badboy录制的脚本有setp ...

  5. GO语言学习(三)GO语言学习API文档

    一:GoLang标准库API文档 https://studygolang.com/pkgdoc

  6. js中的$符号代表什么

    js中的$符号代表什么 一.总结 1.$:相当于document.getElementById(...) 2.$常用用法:每句话意思下面有,好东西 $("div p"); // ( ...

  7. 2、opencv2.4.13.6安装

    一. 卸载opencv3.3.0: Going to the "build" folder directory of opencv from terminal, and execu ...

  8. 【5001】n皇后问题

    Time Limit: 10 second Memory Limit: 2 MB 在n*n的棋盘上放置n个皇后(国际象棋中的皇后,n≤10)而彼此不受攻击(即在棋盘的任一行,任一列和任一对角线上不能放 ...

  9. [Angular] @ContentChild with Directive ref

    For example you have a component, which take a trasclude input element: <au-fa-input id="pas ...

  10. 《SPA设计与架构》之MV*框架

    原文 简书原文:https://www.jianshu.com/p/39f8f0aefdc2 大纲 1.认识MV*框架 2.传统UI设计模式 3.对框架的本质认识——框架有效性和框架分类 4.MV*基 ...