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. kettle 中 java.lang.ClassCastException: [B cannot be cast to java.lang.String报错的解决方法

    问题描述:从数据库中查询出的某字段是json类型数据,然后在json输入步骤报错java.lang.ClassCastException: [B cannot be cast to java.lang ...

  2. Impala查询执行过程

  3. META标签的定义与使用(二、页面描述信息(NAME))

    二.name的content指定实际内容.如:如果指定level(等级)为value(值),则Content可能是beginner(初级).intermediate(中级).advanced(高级). ...

  4. rem换算公式

    当前rem基准值=预设的基准值/设计稿宽度*当前设备的宽度

  5. canvas填充规则,非零环绕

    1.看一块区域是否填充 2.从这个区域拉一条直线 3,看和这条直线相交的轨迹 4.如果顺时针轨迹+1 5.如果逆时针轨迹-1 6.所有轨迹的值计算出来 7.如果是非0,那么填充 8.如果是0那么不填充

  6. load data local infile

    发财 基本语法:load data [low_priority] [local] infile '文件名称' [replace替换策略 | ignore忽略策略]into table 表名称[fiel ...

  7. 校园商铺-2项目设计和框架搭建-6逐层完成SSM的各项配置

    1. 创建/src/main/resources/jdbc.properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://l ...

  8. JS函数进阶

    函数的定义方式 函数声明 函数表达式 new Function 函数声明  function foo () { } 函数表达式  var foo = function () { ​ } 函数声明与函数 ...

  9. LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

    传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...

  10. Vue.js Ajax(axios)

    Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求. Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中. Github开源地址: ht ...