系统获取 IP 工具类

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List; /**
* 系统获取IP工具类
*/
public final class IPUtil { /**
* 取到当前机器的IP地址,这里可以直接获取该服务器的所有网卡ip,如果包括内外网网卡,就是两个ip,中间以,分隔。
*/
public static String getIp() {
String hostIp = null;
List<String> ips = new ArrayList<String>();
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
Enumeration<InetAddress> inteAddresses = netInterface.getInetAddresses();
while (inteAddresses.hasMoreElements()) {
InetAddress inetAddress = inteAddresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
ips.add(inetAddress.getHostAddress());
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
hostIp = collectionToDelimitedString(ips, ",");
return hostIp;
} /**
* 集合转化为连接字符串
*/
private static String collectionToDelimitedString(Collection<String> coll, String delim) {
if (coll == null || coll.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
Iterator<?> it = coll.iterator();
while (it.hasNext()) {
sb.append(it.next());
if (it.hasNext()) {
sb.append(delim);
}
}
return sb.toString();
} /**
* 获取服务器名称
*/
public static String getHostName() {
String hostName = null;
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
e.fillInStackTrace();
}
return hostName;
} public static void main(String[] args) {
System.out.println(IPUtil.getIp());
System.out.println(IPUtil.getHostName());
String[] strArr = {"Google", "Baidu", "IBM", "Github", "Stackoverflow"};
List<String> list = Arrays.asList(strArr);
String result = collectionToDelimitedString(list, "、");
System.out.println(result);
}
}

运行结果

```
192.168.50.116
DESKTOP-C6NCOA8
Google、Baidu、IBM、Github、Stackoverflow
```

系统获取 IP 工具类的更多相关文章

  1. Java 获取IP工具类、Vo类整理记录

    前言 日常开发中,获取ip是常用的功能,本文记录如何在Java中获取本机外网ip.地理位置,访问用户的外网ip.地理位置,以及指定外网ip的地理位置: 代码编写 1.获取访问用户外网ip,我们从访问者 ...

  2. IP工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...

  3. Android 获取手机的厂商、型号、Android系统版本号等工具类(转载)

    Android 获取手机的厂商.型号.Android系统版本号等工具类 1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android系统版本号 5.获取手机IMEI串 ...

  4. java获取真实ip工具类

    场景 有的时候我们需要获取客户端的真实ip,用来实现ip白名单,和黑名单的配置! ip工具类如下 package com.meeno.framework.utils; import javax.ser ...

  5. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...

  6. IP工具类-自己动手做个ip解析器

    IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:

  7. Spring获取bean工具类,可用于在线程里面获取bean

    Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...

  8. c# 系统校时工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HuaT ...

  9. Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类

    最近在开发中,需要用到一些系统信息,这里我把这些方法写成一个工具类方便以后复用,该工具类有以下6个功能: 1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android ...

随机推荐

  1. DOM Composition 事件

    做实时的表单表单校验时,如果输入的是非拉丁语言,那你可能会遇到下面的问题: 如上图所示,文本框不允许输入 ' 之类的特殊字符,当用户在敲击拼音.还未最终输入时就已经触发了校验,提示输入不合法,有点尴尬 ...

  2. dashucoding记录2019.6.8

    WordPress网站 网址: https://cn.wordpress.org/ 阿里云市场 https://market.aliyun.com/products/53616009?spm=a2c4 ...

  3. mac 安装 mysql 5.7

    下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下一步,经过一系列安装步骤后,会跳出一个这样的界面,请注意!!! 上面红框中是你 ...

  4. PHP + CI框架 + AdminLITE权限管理系统

    源码地址:https://github.com/yangsphp/rbac-master

  5. html 获取地址栏信息

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  6. 深度学习: 学习率 (learning rate)

    Introduction 学习率 (learning rate),控制 模型的 学习进度 : lr 即 stride (步长) ,即反向传播算法中的 ηη : ωn←ωn−η∂L∂ωnωn←ωn−η∂ ...

  7. PHP fopen/file_get_contents与curl性能比较

    PHP中fopen,file_get_contents,curl 函数的区别: 1.fopen/file_get_contents 每次请求都会重新做 DNS 查询,并不对 DNS 信息进行缓存. 但 ...

  8. maven报错解决

    maven-resources-plugin prior to 2.4 is not supported by m2e. Use maven-  resources-plugin versio < ...

  9. [Java复习] 分布式锁 Zookeeper Redis

    一般实现分布式锁都有哪些方式? 使用 Redis 如何设计分布式锁?使用 Zookeeper 来设计分布式锁可以吗? 这两种分布式锁的实现方式哪种效率比较高? 1. Zookeeper 都有哪些使用场 ...

  10. nginx限制流量

    location /download/ { limit_rate_after 5m; limit_rate 1m; alias /data/html/; }