zoukankan      html  css  js  c++  java
  • java 获取本机所有IP地址

    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", 0);
    %>
    
    <!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="0">    
        
      </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="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>
  • 相关阅读:
    JZ36 两个链表的第一个公共结点
    程序员的表达能力
    Git学习(2)-使用Git 代码将本地文件提交到 GitHub
    初识模块
    三元表达式、递归、匿名函数
    CSRF
    XSS前置课程--同源策略
    XSS
    SQL注入基础入门
    Linux下ettercap的安装,make安装软件步骤
  • 原文地址:https://www.cnblogs.com/hzm112567/p/4221610.html
Copyright © 2011-2022 走看看