import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; public class GetLocalIpAddress {
public static void main(String[] args) {
Map<String, List<String>> map = getLocalIP();
for(String key : map.keySet()) {
System.out.println(key + ": " + map.get(key).toString());
}
} public static Map<String, List<String>> getLocalIP() {
Map<String, List<String>> map = new HashMap<String, List<String>>();
Enumeration<NetworkInterface> e1;
try {
e1 = NetworkInterface.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = e1.nextElement(); List<String> ips = new LinkedList<String>();
map.put(ni.getName(), ips); Enumeration<InetAddress> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = e2.nextElement();
if(ia instanceof Inet6Address) {
continue; // omit IPv6 address
}
ips.add(ia.getHostAddress());
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
}
output:
lo: [127.0.0.1]
eth0: [192.168.2.68, 192.168.2.67]
<%@ page language="java" import="java.util.*, java.net.*, java.text.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", );
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content=""> </head>
<body> <%!
public static Map<String, List<String>> getLocalIP() {
Map<String, List<String>> map = new HashMap<String, List<String>>();
Enumeration<NetworkInterface> e1;
try {
e1 = NetworkInterface.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = e1.nextElement(); List<String> ips = new LinkedList<String>();
map.put(ni.getName(), ips); Enumeration<InetAddress> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = e2.nextElement();
if(ia instanceof Inet6Address) {
continue; // omit IPv6 address
} else {
ips.add(ia.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
%> <div style="width:60%; margin: 0 auto;">
<div>You access the server: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()) %></div>
<%
Map<String, List<String>> map = getLocalIP();
for(String key : map.keySet()) {
%>
<div style="padding-left: 30px;"><%=key + ": " + map.get(key).toString() %></div>
<%
}
%> </div> </body>
</html>

java 获取本机所有IP地址的更多相关文章

  1. java 获取本机的IP地址

    方法一:这种方式有一定的局限性,在Linux下的执行结果是:本机的IP = xxx/127.0.1.1 (其中xxx是你的计算机名) public void getLocalIPAddress() { ...

  2. Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP

    Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP IP 工具类 源代码: /** * <p> * * @author XiaoPengwei ...

  3. 【Win 10 应用开发】获取本机的IP地址

    按照老规矩,也是朋友的建议,老周今天在吹牛之前,先讲一个小故事. 有朋友问我,老周,你现在还发短信吗,你每个月用多少电话费?唉,实话说,现在真的发短信不多了,套餐送的130条短信,每月都发不了一条.至 ...

  4. 获取本机的IP地址(局域网)与主机名称

    编写内容保存为bat @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址(局域网)=========*/ e ...

  5. Java 获取客服端ip地址

    Java 获取客服端ip地址 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p& ...

  6. 获取本机的IP地址和mac地址

    1. 以前一直用ipconfig来查看ip地址,哈哈哈,现在发现挺好玩 #获取本机的IP地址和mac地址 import uuid import socket def get_mac_address() ...

  7. 获取本机的ip地址(排除虚拟机,蓝牙等ip)

    项目中遇到了要获取本地ip的需求,网上查找资料遇到很多坑,很多Java获取本机ip地址的方法要么是根本获取不到,要么是获取的有问题. 网上常见的方法如下 InetAddress.getLocalHos ...

  8. C++获取本机的ip地址程序

    #include <WinSock2.h> #pragma comment(lib,"ws2_32") //链接到ws2_32动态链接库 class CInitSock ...

  9. JAVA获取本机的MAC地址

    /** * 获取本机的Mac地址 * @return */ public String getMac() { InetAddress ia; byte[] mac = null; try { // 获 ...

随机推荐

  1. Nginx:413 Request Entity Too Large 的解决方法

    报错信息413 Request Entity Too Large 解决方法: (20M大小,自己调节,根据文件大小.)修改 php 的配置文件 /etc/php5/fpm/php.ini upload ...

  2. Array类型中的检测数组,转换方法,栈方法,队列方法

    我的新博客==> http://www.suanliutudousi.com/2017/08/24/array%E7%B1%BB%E5%9E%8B%E4%B8%AD%E7%9A%84%E6%A3 ...

  3. 7-MySQL高级-主从-1

    1. 主从同步的定义 主从同步使得数据可以从一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave). 因为复制是异步进行的,所 ...

  4. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  5. Centos7 pxe

    yum install dnsmasq mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup # vim /etc/dnsmasq.conf interface= ...

  6. js添加节点

    <!DOCTYPE html><html><body><script>var message=document.createTextNode(" ...

  7. 安装Epson打印机但因lsb依赖错误而中断的驱动程序

    sudo apt-get install printer-driver-escpr 安装Epson打印机但因lsb依赖错误而中断的驱动程序 问题: 我正在安装 Epson XP-310 驱动程序,从这 ...

  8. C++: class sizeof

    https://blog.csdn.net/fengxinlinux/article/details/72836199 C++中类所占的大小计算,因为涉及到虚函数成员,静态成员,虚继承,多继承以及空类 ...

  9. excel 导数据

    参考: ="insert tsilverinfo(ss_id,memo,ss_weight,ts_id,ss_type,ModelPosX,ss_stoneW,ss_stoneWU) val ...

  10. Android开发 LiveData与MutableLiveData详解

    前言 LiveData与ViewMode是经常搭配在一起使用的,但是为了不太混乱,我还是拆分开来说明,此篇博客只讲解 LiveData 与 MutableLiveData的概念与使用方式(但是会涉及到 ...