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="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>

 

posted on 2015-01-13 16:06  hzm_frank  阅读(545)  评论(0编辑  收藏  举报

导航