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