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>
  • 相关阅读:
    [状压DP]JZOJ 3293 阶乘字符串
    [线段树][Splay][树状数组]JZOJ 3292 发牌
    MySQL之索引
    MySQL(二)
    mysql 练习题
    前端 jQuery
    关于vertical-align和line-height的真知灼见
    CSS语法
    HTML初识
    HTTP协议
  • 原文地址:https://www.cnblogs.com/hzm112567/p/4221610.html
Copyright © 2011-2022 走看看