美文网首页Java web
java获取ip,浏览器类型,mac地址,数组型字符串转数组

java获取ip,浏览器类型,mac地址,数组型字符串转数组

作者: haiyong6 | 来源:发表于2019-06-28 11:31 被阅读0次
    /**
         * 获取外网IP
         * 
         * @param request
         * @return
         */
        public static String getRemoteHost(HttpServletRequest request) {
            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.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
        }
        
        /**
         * 
         * 获取浏览器类型
         * @param request
         * @return
         */
        public static String getRequestBrowserInfo(HttpServletRequest request){
            String browserType = null;
            String header = request.getHeader("user-agent");
            if(header == null || header.equals("")){
               return "";
             }
            if(header.indexOf("MSIE")>0){
                browserType = "IE";
            }else if(header.indexOf("Firefox")>0){
                browserType = "Firefox";
            }else if(header.indexOf("Chrome")>0){
                browserType = "Chrome";
            }else if(header.indexOf("Safari")>0){
                browserType = "Safari";
            }else if(header.indexOf("Camino")>0){
                browserType = "Camino";
            }else if(header.indexOf("Konqueror")>0){
                browserType = "Konqueror";
            }
            return browserType;
        }
        
        
        /**
         * 
         * 获取用户mac地址
         * @param ip
         * @return
         */
        public static String getMacAddrByIp(String ip) {
            String macAddr = null;
            try {
                Process process = Runtime.getRuntime().exec("nbtstat -a " + ip);
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
                Pattern pattern = Pattern.compile("([A-F0-9]{2}-){5}[A-F0-9]{2}");
                Matcher matcher;
                for (String strLine = br.readLine(); strLine != null;
                     strLine = br.readLine()) {
                    matcher = pattern.matcher(strLine);
                    if (matcher.find()) {
                        macAddr = matcher.group();
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return macAddr;
        }
    
    /**
         * 数组型字符串转数组如 ["1,2","3"]
         */
        public static String[] strToArray(String str) {
            List arrayList = new ArrayList();
            String strNew = str.replace("[\"", "").replace("\"]", "").replace("\\/", "/");
            String[] strs = strNew.split("\",\"");
            return strs;
        }
    

    相关文章

      网友评论

        本文标题:java获取ip,浏览器类型,mac地址,数组型字符串转数组

        本文链接:https://www.haomeiwen.com/subject/nyrscctx.html