综合了网上找的代码,整理的,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. 【AtCoder Beginner Contest 074 B】Collecting Balls (Easy Version)

    [链接]h在这里写链接 [题意] 看懂题目之后就会发现是道大水题. [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h&g ...

  2. UVA 10943 - How do you add? 递推

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. LA 3135 - Argus

    看题:传送门 大意就是让你编写一个称为argus的系统,这个系统支持一个register的命令: Register  Q_num Period 该命令注册了一个触发器,它每Period秒就会残生一个编 ...

  4. 中间件、服务器和Web服务器三者的区别

    相信很多的Web安全初学者和我一样,对中间件和服务器的认识不够深刻,对两者的概念可能会有所混淆. 正好今天在学习的时候突然想到了这个问题,粗略百度了一下,似乎网上对这个问题的解释不多,那么就由我来为大 ...

  5. ASI使用

    一.ASI类库集成: .添加源代码文件 ASIAuthenticationDialog.h ASIAuthenticationDialog.m ASICacheDelegate.h ASIDataCo ...

  6. Android ViewGroup使用小结

    ViewGroup定义 在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我 ...

  7. Java反射学习总结终(使用反射和注解模拟JUnit单元测试框架)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 本文是Java反射学习总结系列的最后一篇了,这里贴出之前文章的链接,有兴趣的可以打开看看. ...

  8. [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid

    For each formBuild, formControl, formGroup they all have 'valueChanges' prop, which is an Observable ...

  9. php 下载图片到服务器

    function saveImage($path) { if(!preg_match('/\/([^\/]+\.[a-z]{3,4})$/i',$path,$matches)) die('Use im ...

  10. POJ 1287 Networking (ZOJ 1372) MST

    http://poj.org/problem?id=1287 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=372 和上次那题差 ...