殇丶善若水个人页面-阿里云开发者社区

个人头像照片 殇丶善若水 TA的个人档案

个人介绍

暂无个人介绍

擅长的技术

  • Java
  • Linux
  • 数据库
获得更多能力
通用技术能力:
  • Java
    初级

    能力说明:

    了解变量作用域、Java类的结构,能够创建带main方法可执行的java应用,从命令行运行java程序;能够使用Java基本数据类型、运算符和控制结构、数组、循环结构书写和运行简单的Java程序。

    获取记录:

云产品技术能力:

暂时未有相关云产品技术能力~

阿里云技能认证

详细说明
  • 高分内容
  • 最新动态
  • 文章
  • 问答
正在加载, 请稍后...
滑动查看更多

2019年08月

  • 08.06 09:38:17
    回答了问题 2019-08-06 09:38:17

    java如何获取发起请求的用户真实IP

    /**
         * 获取用户实际IP地址
         * @param request 当前请求对象
         * @return 实际IP地址
         */
        public static String getRemoteIp(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            logger.trace("当前IP来源[X-Forwarded-For], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                //多次反向代理后会有多个ip值,第一个ip才是真实ip
                int index = ip.indexOf(',');
                if(index != -1){
                    return ip.substring(0, index);
                }else{
                    return ip;
                }
            }
            ip = request.getHeader("X-Real-IP");
            logger.trace("当前IP来源[X-Real-IP], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                return ip;
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                logger.trace("当前IP来源[Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                logger.trace("当前IP来源[WL-Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                logger.trace("当前IP来源[HTTP_CLIENT_IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                logger.trace("当前IP来源[HTTP_X_FORWARDED_FOR], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                logger.trace("当前IP来源[getRemoteAddr], 值[{}]", ip);
            }
            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                String ipv4FromLocal = getIpv4FromLocal();
                if (StringUtils.isNotEmpty(ipv4FromLocal)) {
                    ip = ipv4FromLocal;
                }
            }
            return ip;
        }
    
        /**
         * 获取本地IP地址
         * @return IP地址
         */
        private static String getIpv4FromLocal() {
            String ip = null;
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                Process process = Runtime.getRuntime().exec("cmd.exe /c ipconfig | findstr IPv4");
                is = process.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String line = br.readLine();
                ip = line.substring(line.indexOf(':') + 1).trim();
            } catch (IOException e) {
                logger.warn("获取本地IP异常", e);
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    logger.debug("流关闭异常", e);
                }
            }
            return ip;
        }
    
    踩0 评论0
  • 08.06 09:37:18
    回答了问题 2019-08-06 09:37:18

    请教下,Java 获取本机ip 怎么将虚拟网卡排除掉啊,有什么方式吗?

    /**
         * 获取用户实际IP地址
         * @param request 当前请求对象
         * @return 实际IP地址
         */
        public static String getRemoteIp(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            logger.trace("当前IP来源[X-Forwarded-For], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                //多次反向代理后会有多个ip值,第一个ip才是真实ip
                int index = ip.indexOf(',');
                if(index != -1){
                    return ip.substring(0, index);
                }else{
                    return ip;
                }
            }
            ip = request.getHeader("X-Real-IP");
            logger.trace("当前IP来源[X-Real-IP], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                return ip;
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                logger.trace("当前IP来源[Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                logger.trace("当前IP来源[WL-Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                logger.trace("当前IP来源[HTTP_CLIENT_IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                logger.trace("当前IP来源[HTTP_X_FORWARDED_FOR], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                logger.trace("当前IP来源[getRemoteAddr], 值[{}]", ip);
            }
            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                String ipv4FromLocal = getIpv4FromLocal();
                if (StringUtils.isNotEmpty(ipv4FromLocal)) {
                    ip = ipv4FromLocal;
                }
            }
            return ip;
        }
    
        /**
         * 获取本地IP地址
         * @return IP地址
         */
        private static String getIpv4FromLocal() {
            String ip = null;
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                Process process = Runtime.getRuntime().exec("cmd.exe /c ipconfig | findstr IPv4");
                is = process.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String line = br.readLine();
                ip = line.substring(line.indexOf(':') + 1).trim();
            } catch (IOException e) {
                logger.warn("获取本地IP异常", e);
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    logger.debug("流关闭异常", e);
                }
            }
            return ip;
        }
    
    踩0 评论0
正在加载, 请稍后...
滑动查看更多
  • 发表了文章 2018-11-22

    Linux下安装ActiveMQ-5.15.8

  • 发表了文章 2018-11-13

    Java递归实现简单算法

  • 发表了文章 2018-11-04

    Mybatis分页插件PageHelper-5.1.1

  • 发表了文章 2018-10-25

    Linux常用命令

  • 发表了文章 2018-10-19

    Linux下安装Tomcat 8.0.53

  • 发表了文章 2018-10-16

    Linux下yum安装MySQL

  • 发表了文章 2018-10-16

    Linux下安装jdk

正在加载, 请稍后...
滑动查看更多
  • 回答了问题 2019-08-06

    java如何获取发起请求的用户真实IP

    /**
         * 获取用户实际IP地址
         * @param request 当前请求对象
         * @return 实际IP地址
         */
        public static String getRemoteIp(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            logger.trace("当前IP来源[X-Forwarded-For], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                //多次反向代理后会有多个ip值,第一个ip才是真实ip
                int index = ip.indexOf(',');
                if(index != -1){
                    return ip.substring(0, index);
                }else{
                    return ip;
                }
            }
            ip = request.getHeader("X-Real-IP");
            logger.trace("当前IP来源[X-Real-IP], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                return ip;
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                logger.trace("当前IP来源[Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                logger.trace("当前IP来源[WL-Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                logger.trace("当前IP来源[HTTP_CLIENT_IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                logger.trace("当前IP来源[HTTP_X_FORWARDED_FOR], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                logger.trace("当前IP来源[getRemoteAddr], 值[{}]", ip);
            }
            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                String ipv4FromLocal = getIpv4FromLocal();
                if (StringUtils.isNotEmpty(ipv4FromLocal)) {
                    ip = ipv4FromLocal;
                }
            }
            return ip;
        }
    
        /**
         * 获取本地IP地址
         * @return IP地址
         */
        private static String getIpv4FromLocal() {
            String ip = null;
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                Process process = Runtime.getRuntime().exec("cmd.exe /c ipconfig | findstr IPv4");
                is = process.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String line = br.readLine();
                ip = line.substring(line.indexOf(':') + 1).trim();
            } catch (IOException e) {
                logger.warn("获取本地IP异常", e);
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    logger.debug("流关闭异常", e);
                }
            }
            return ip;
        }
    
    踩0 评论0
  • 回答了问题 2019-08-06

    请教下,Java 获取本机ip 怎么将虚拟网卡排除掉啊,有什么方式吗?

    /**
         * 获取用户实际IP地址
         * @param request 当前请求对象
         * @return 实际IP地址
         */
        public static String getRemoteIp(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            logger.trace("当前IP来源[X-Forwarded-For], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                //多次反向代理后会有多个ip值,第一个ip才是真实ip
                int index = ip.indexOf(',');
                if(index != -1){
                    return ip.substring(0, index);
                }else{
                    return ip;
                }
            }
            ip = request.getHeader("X-Real-IP");
            logger.trace("当前IP来源[X-Real-IP], 值[{}]", ip);
            if(!StringUtils.isEmpty(ip) && !NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)){
                return ip;
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                logger.trace("当前IP来源[Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                logger.trace("当前IP来源[WL-Proxy-Client-IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                logger.trace("当前IP来源[HTTP_CLIENT_IP], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                logger.trace("当前IP来源[HTTP_X_FORWARDED_FOR], 值[{}]", ip);
            }
            if (StringUtils.isEmpty(ip) || NoticeConstant.UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                logger.trace("当前IP来源[getRemoteAddr], 值[{}]", ip);
            }
            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                String ipv4FromLocal = getIpv4FromLocal();
                if (StringUtils.isNotEmpty(ipv4FromLocal)) {
                    ip = ipv4FromLocal;
                }
            }
            return ip;
        }
    
        /**
         * 获取本地IP地址
         * @return IP地址
         */
        private static String getIpv4FromLocal() {
            String ip = null;
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                Process process = Runtime.getRuntime().exec("cmd.exe /c ipconfig | findstr IPv4");
                is = process.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                String line = br.readLine();
                ip = line.substring(line.indexOf(':') + 1).trim();
            } catch (IOException e) {
                logger.warn("获取本地IP异常", e);
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    logger.debug("流关闭异常", e);
                }
            }
            return ip;
        }
    
    踩0 评论0
正在加载, 请稍后...
滑动查看更多