zoukankan      html  css  js  c++  java
  • 在页面获取本地电脑IP

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String ctx = request.getContextPath();
        request.setAttribute("ctx", ctx);
    %>
    <!-- <html lang="en" style="min-height: 100%;overflow: hidden">
    <head lang="zh-cn">
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     <meta content="MSHTML 6.00.2800.1106" name="GENERATOR">
    </head>
    <body> -->
        
        <!--<form id="formfoo" name="formbar" action="#" method="post" style="display:none">
            <input value="00:05:5D:0E:C7:FA" name="txtMACAddr">
            <input value="192.168.30.68" name="txtIPAddr">
            <input value="lzf" name="txtDNSName">
        </form>-->
    <!-- </body>
    </html> -->
    
    <!--[if !IE]> -->
    <!-- <![endif]-->
    
    <script type="text/javascript">
    
    $(function () {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isOpera = userAgent.indexOf("Opera") > -1;
        if (userAgent.indexOf("Chrome") > -1){
            gooleGetIP();
            return "Chrome";
        }
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            ieGetIP();
            return "IE";
        }; 
    });
    function gooleGetIP(){
        function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
              //compatibility for firefox and chrome
              var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
              var pc = new myPeerConnection({
                 iceServers: []
             }),
             noop = function() {},
             localIPs = {},
             ipRegex = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
             key;
         
             function iterateIP(ip) {
                 if (!localIPs[ip]) onNewIP(ip);
                 localIPs[ip] = true;
            }
         
              //create a bogus data channel
             pc.createDataChannel("");
         
             // create offer and set local description
             pc.createOffer().then(function(sdp) {
                 sdp.sdp.split('
    ').forEach(function(line) {
                     if (line.indexOf('candidate') < 0) return;
                     line.match(ipRegex).forEach(iterateIP);
                 });
                 
                 pc.setLocalDescription(sdp, noop, noop);
             });
         
             //sten for candidate events
             pc.onicecandidate = function(ice) {
                 if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
                 ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
             };
        }
         
        // Usage
         
        getUserIP(function(ip){
            //alert("Got IP! :" + ip);
            $.ajax({
                url:"${ctx}/system/saveUserLoginIP",
                data:{loginIp:ip},
                type:"post",
                dataType:"json",
                async:false, //同步
                success:function(data){
                    console.log(data.data);
                },
                error:function(data){
                    alert(data);
                }
            });
        }); 
    };
    
    function ieGetIP() {
    
    }
    </script>
    
    
    <object id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></object>
    <object id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></object>
    
    <script language="JScript">
        var service = locator.ConnectServer();
        var MACAddr;
        var IPAddr;
        var DomainAddr;
        var sDNSName;
        service.Security_.ImpersonationLevel = 3;
        service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
    </script>
    <script language="JScript" event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for="foo">
       /*  document.forms[0].txtMACAddr.value = unescape(MACAddr);
        document.all.txtIPAddr.value = unescape(IPAddr);
        document.forms[0].txtDNSName.value = unescape(sDNSName); */
        //alert(unescape(IPAddr));
        $.ajax({
            url:"${ctx}/system/saveUserLoginIP",
            data:{loginIp:unescape(IPAddr)},
            type:"post",
            dataType:"json",
            async:false, //同步
            success:function(data){
                console.log(data.data);
            },
            error:function(data){
                alert(data);
            }
        });
    </script>
    <script language="JScript" event="OnObjectReady(objObject,objAsyncContext)" for="foo">
        if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true) {
            if(objObject.MACAddress != null && objObject.MACAddress != "undefined") {
                MACAddr = objObject.MACAddress;
            }
            if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") {
                IPAddr = objObject.IPAddress(0);
            }
            if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined") {
                sDNSName = objObject.DNSHostName;
            }
        }
    </script>
  • 相关阅读:
    2017.6.30 码云--生成公钥
    2017.6.30 IDEA插件--gsonfomat的安装与使用
    2017.6.30 安装IDEA的插件mybatis plugin(破解版)
    2017.6.29 java读取.properties配置文件的几种方法
    2017.6.29 移除再导入maven module到IDEA中时提示: Unable to proceed. Nothing found to import.
    2017.6.27 跟开涛学spring3--spring概述
    2017.6.27 jdbc基本使用
    2017.6.26 接口测试工具postman使用总结
    16-20
    11-15
  • 原文地址:https://www.cnblogs.com/lijianda/p/11212419.html
Copyright © 2011-2022 走看看