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;
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java使用POST方法传递参数,可以使用以下步骤: 1. 创建一个URL对象,指向目标URL地址 2. 打开连接,调用URLConnection对象的`URLConnection.openConnection()`方法 3. 设置请求方法为POST,调用URLConnection对象的`URLConnection.setRequestMethod("POST")`方法 4. 设置请求属性,调用URLConnection对象的`URLConnection.setRequestProperty()`方法,设置Content-Type、User-Agent等参数 5. 开启输出流,调用URLConnection对象的`URLConnection.setDoOutput(true)`方法,表示可以向服务器写入数据 6. 获取输出流,调用URLConnection对象的`URLConnection.getOutputStream()`方法 7. 向输出流写入数据,即要传递参数,可以使用PrintWriter或BufferedWriter等输出流 8. 关闭输出流,调用输出流的`close()`方法 9. 获取输入流,调用URLConnection对象的`URLConnection.getInputStream()`方法 10. 读取输入流的数据,即服务器返回的响应结果 11. 关闭输入流和连接,调用输入流和连接的`close()`方法 以下是示例代码: ``` URL url = new URL("http://example.com"); URLConnection connection = url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); writer.print("param1=value1"); writer.print("&param2=value2"); writer.flush(); writer.close(); os.close(); InputStream is = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); is.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值