JAVA 获取get、post 请求的参数

package com.banxia.configcenter.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackagName com.banxia.configcenter.util
 * @Classname RequestDataUtils
 * @Description 获取http 请求数据
 * @Date 2019-03-10 16:43
 * @Created by banxia
 */
public class RequestDataUtils {

    /**
     * @Author banxia
     * @Description //获取请求类型
     * @Date 16:46 2019-03-10
     * @Param [http]
     * @return java.lang.String
     **/
    public static String getMethod(HttpServletRequest http) {
        return http.getMethod();
    }

    /**
     * @Author banxia
     * @Description //获取url 上的参数
     * @Date 16:42 2019-03-10
     * @Param
     * @return com.alibaba.fastjson.JSONObject
     **/
    public static JSONObject getData(HttpServletRequest http) {
        Map<String, String[]> parameterMap = http.getParameterMap();
        JSON.toJSON(parameterMap);
        JSONObject jsonObject = new JSONObject();
        for (String key : parameterMap.keySet()) {
            String[] values = parameterMap.get(key);
            jsonObject.put(key, values[0]);
        }
        return jsonObject;
    }

    /**
     * @Author banxia
     * @Description //获取url+ post 的数据
     * @Date 16:45 2019-03-10
     * @Param [http]
     * @return java.util.HashMap<java.lang.String,com.alibaba.fastjson.JSONObject>
     **/
    public static HashMap<String, JSONObject> request(HttpServletRequest http) {
        HashMap<String, JSONObject> result = new HashMap<>();
        result.put("postData", postData(http));
        result.put("getData", getData(http));
        return result;
    }

    /**
     * @Author banxia
     * @Description //获取post 数据
     * @Date 16:47 2019-03-10
     * @Param [http]
     * @return com.alibaba.fastjson.JSONObject
     **/
    public static JSONObject postData(HttpServletRequest http) {
        StringBuffer data = dealInputStream(http);
        return JSON.parseObject(data.toString());
    }

    /**
     *
     * @param http
     * @return
     */
    public static StringBuffer dealInputStream(HttpServletRequest http) {
        StringBuffer data = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
            String s;
            while ((s = reader.readLine()) != null) {
                data.append(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}

DEMO:

    @RequestMapping("/http2")
    public String http2(HttpServletRequest http) {
        //获取请求方法
        Object method = RequestDataUtils.getMethod(http);

        HashMap<String, JSONObject> request = RequestDataUtils.request(http);

        HashMap<String, Object> result = new HashMap<>();
        //返回值
        System.out.println(request.get("postData").get(request.get("getData").get("key")));
        result.put("method", method);
        result.putAll(request);
        return JSON.toJSONString(result);
    }

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值