zoukankan      html  css  js  c++  java
  • Java开发中的一些小技巧

    原文:http://www.cnblogs.com/xdp-gacl/p/3490276.html

    一、 Java获取URL地址中传递的参数

    /**
         * 获取URL中的参数名和参数值的Map集合
         * @param url
         * @return
         */
        private Map<String, String> getUrlPramNameAndValue(String url){
        String regEx="(\?|&+)(.+?)=([^&]*)";//匹配参数名和参数值的正则表达式
            Pattern p = Pattern.compile(regEx);  
            Matcher m = p.matcher(url);
         // LinkedHashMap是有序的Map集合,遍历时会按照加入的顺序遍历输出
        Map<String, String> paramMap = new LinkedHashMap<String, String>();
            while(m.find()){
            String paramName = m.group(2);//获取参数名
            String paramVal=m.group(3);//获取参数值
                paramMap.put(paramName, paramVal);
            }
            return paramMap;
        }

    二、获取请求的URL地址

    /**
         * 获取请求的URL地址
         * @return
         */
        public String getRequestUrl(){
        HttpServletRequest request = ServletActionContext.getRequest();
         // request.getRequestURL()获取到的是不带参数的URL,request.getQueryString()获取到的是URL的参数部分,要想获取带参数的完整URL,就需要把这两部分拼凑起来
        String url = request.getRequestURL()+"?"+request.getQueryString();
        return url;
            
        }

    三、获取请求的IP地址

    /**
         * 获取请求的IP地址
         * @return
         */
        public String getRequestIpAddress(){
            return ServletActionContext.getRequest().getRemoteAddr();
        }

    四:判断字符串是否能够转换成指定格式的日期

    /**
        * 验证字符串是否能够转换成指定格式的日期
        * @param str
        * @return date
        */
        public static boolean isValidDate(String str ,String formater) {
          boolean convertSuccess=true;
           SimpleDateFormat format = new SimpleDateFormat(formater);
           try {
              format.setLenient(false);
              format.parse(str);
           } catch (ParseException e) {
              // e.printStackTrace();
              //如果throw java.text.ParseException或者NullPointerException,就说明格式不对
               convertSuccess=false;
           } 
           return convertSuccess;
        }
  • 相关阅读:
    FreeMarker的<#if></#if>标签
    ubuntu的dpkg命令安装和卸载软件
    ubuntu建立软链接注意事项
    halo的工作目录,有一个是在代码里配置的,硬编码了
    Springboot的多环境配置
    idea中的springboot+gradle项目报错springboot configuration annotation processor not found in classpath
    maven中的pom.xml中的scope的作用
    设置idea的快捷键组合 设置为默认
    springboot无法查询到后台的数据
    ssh互信条件下的多机拷贝脚本和执行远程命令
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6210626.html
Copyright © 2011-2022 走看看