日期:2014-05-17  浏览次数:20786 次

获取本机iP
在项目中,我要做一个考勤模块,需要保存本机的iP到数据库,请问如何获取到ip,使用js脚本可以获取到吗

------解决方案--------------------
百度一下 很多
------解决方案--------------------
Java code

        String ip = request.getHeader("x-forwarded-for");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }

------解决方案--------------------
Java code
public static String getRealIp() throws SocketException {

        String localip = null;// 本地IP,如果没有配置外网IP则返回它

        String netip = null;// 外网IP

        Enumeration<NetworkInterface> netInterfaces =

        NetworkInterface.getNetworkInterfaces();

        InetAddress ip = null;

        boolean finded = false;// 是否找到外网IP

        while (netInterfaces.hasMoreElements() && !finded) {

            NetworkInterface ni = netInterfaces.nextElement();

            Enumeration<InetAddress> address = ni.getInetAddresses();

            while (address.hasMoreElements()) {

                ip = address.nextElement();

                if (!ip.isSiteLocalAddress()

                && !ip.isLoopbackAddress()

                && ip.getHostAddress().indexOf(":") == -1) {// 外网IP

                    netip = ip.getHostAddress();

                    finded = true;

                    break;

                } else if (ip.isSiteLocalAddress()

                && !ip.isLoopbackAddress()

                && ip.getHostAddress().indexOf(":") == -1) {// 内网IP

                    localip = ip.getHostAddress();

                }

            }

        }

        if (netip != null && !"".equals(netip)) {

            return netip;

        } else {
            return localip;

        }

    }

------解决方案--------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript获取客户端IP</title>
</head>
 
<body>
<script type="text/javascript" language="javascript">
<!--
function GetLocalIPAddress()
{
var obj = null;
var rslt = "";
try
{
obj = new ActiveXObject("rcbdyctl.Setting");
rslt = obj.GetIPAddress;
obj = null;
}
catch(e)
{
//异常发生
}

return rslt;
}
 
document.write("你的IP是:" + GetLocalIPAddress());
//-->
</script>
</body>
</html>