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. webService服务简单实现

    首先写一个简单的webservice服务 package com.service.impl; import java.util.Date; import javax.jws.WebService; i ...

  2. SpringBoot-集成PageHelper5.1.2踩坑

    背景就不介绍了,项目是SpringBoot+MyBatis搭建的,需要集成git上的PageHelper5.1.2,这个插件大家都比较熟悉了 之前一直用的PageHelper4.0.3,集成是这样的: ...

  3. 来个我一起学习Python把!!!(新手共同努力)

    <初识Python> 大家好,让我们一起来学习Python,因本人也是个新手但我会把我所学的东西分享出来,并记录自己的经验学习过程,不单单是分享代码,会详细的讲解,如有错误地方希望大家指点 ...

  4. HDU 3308 线段树求区间最长连续上升子序列长度

    题意:两种操作,Q L R查询L - R 的最长连续上升子序列长度,U pos val 单点修改值 #include <bits/stdc++.h> #define N 100005 us ...

  5. jenkins自动化部署jar包(2)

    1.自动化部署流程: svn代码-----jenkins------linux运行 环境: 我这里为了测试:svn,和linux放在阿里云上面.jenkins放在本地windos电脑上运行的 2.下载 ...

  6. Springboot整合dubbo搭建基本的消费、提供和负载均衡

    文章目录 1.确定接口 2.创建提供者 2.1 pom配置 2.2dubbo配置文件 2.3 application.properties 2.4 mybatis相关 2.4.1 配置UserMapp ...

  7. jupyter中使用graphviz

    参考: https://www.cnblogs.com/zhanjiahui/p/11335038.html https://blog.csdn.net/linxid/article/details/ ...

  8. mysql的几种启动方式

    mysql的四种启动方式: 1.mysqld 启动mysql服务器:./mysqld --defaults-file=/etc/my.cnf --user=root 客户端连接: mysql --de ...

  9. gmock 简单笔记

    std::shared_ptr<MockThreadRCInvester> spMockaAcc; HelperThreadRCInvester helperAcc; // spMockA ...

  10. 前端常用的库和实用技术之JavaScript多线程

    多线程概念: 多线程是指从软件或硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在 同一时间执行多于一个线程,进而提升整理处理性能.具有这种能力的系统包括对称多处理机,多核心 ...