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

一、 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地址

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

三、获取请求的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;
    }

猜你喜欢

转载自blog.csdn.net/qq_41954585/article/details/130973357