RequestAnalysisUtil

package com.zichen.xhkq.util.request;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.zichen.xhkq.dict.xhPay.Dict_xhPay_encoding;

public class RequestAnalysisUtil {

	/**
	 * parse Request 2 JsonObject
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	public static JSONObject parseRequest2JsonObject(HttpServletRequest request)
			throws Exception {

		JSONObject jsonObject = new JSONObject();
		// 判断请求method,从request获取参数
		String httpMehtod = request.getMethod();

		// GET请求
		if (httpMehtod.equals("GET")) {
			String requestJsonString = new String(request.getQueryString()
					.getBytes("UTF-8"));
			String str = URLDecoder.decode(requestJsonString, "utf-8");
			// 将get参数转为map,转为json
			Map<String, String> notifyMap = parseString2Map(requestJsonString);

			jsonObject = JSONObject.parseObject(JSONObject
					.toJSONString(notifyMap));

		} else if (httpMehtod.equals("POST")) {

			String requestJsonString = parseHttpRequestPost(request,
					Dict_xhPay_encoding.XHPAY_ENCODING_UTF8);
			String str = URLDecoder.decode(requestJsonString, "utf-8");
			jsonObject = JSONObject.parseObject(str);

		} else {
			throw new Exception("请求方法解析异常");
		}
		return jsonObject;
	}

	/**
	 * 解析post
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static JSONObject parsePost(HttpServletRequest request)
			throws Exception {
		String requestJsonString = parseHttpRequestPost(request,
				Dict_xhPay_encoding.XHPAY_ENCODING_UTF8);
		String requestJsonStr = java.net.URLDecoder.decode(requestJsonString,
				"utf-8");
		// String requestJsonStr = requestJsonString.replaceAll("%22", "\"")
		// .replaceAll("%7B", "{").replaceAll("%7D", "}")
		// .replaceAll("%3A", ":").replaceAll("%2C", ",");
		Map<String, String> parseString2Map = parseString2Map(requestJsonStr);
		JSONObject jsonObject = JSONObject.parseObject(JSONObject
				.toJSONString(parseString2Map));
		return jsonObject;
	}

	/**
	 * string parse 2 MapString
	 * 
	 * @description 以&分割,key=value格式
	 * @author CNZZ
	 * @time 2019-1-5
	 * @param str
	 * @return
	 */
	public static Map<String, String> parseString2Map(String str) {

		Map<String, String> map = new HashMap<String, String>();
		String[] array = str.split("&");
		for (int i = 0; i < array.length; i++) {
			String[] data = array[i].split("=");
			map.put(data[0], data[1]);
		}
		return map;
	}

	/**
	 * 解析request的post
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static String parseHttpRequestPost(HttpServletRequest request,
			String encoding) throws Exception {
		// 请求原文
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					request.getInputStream(), encoding));
			String str = null;
			while ((str = br.readLine()) != null || " ".equals(str)) {
				sb.append(str);
			}
		} catch (IOException e) {
			throw new IOException(e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		String resultStr = sb.toString();
		return resultStr;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值