1. 程式人生 > >request中獲取post的json物件資料content-type=“text/plain”

request中獲取post的json物件資料content-type=“text/plain”


/**
	 *獲取 request 中用POST方式"Content-type"是
	 * "text/plain"傳送的 json資料 
	 * @author: Simon
	 * @date: 2017年8月6日 下午7:44:09
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static String getPostByTextPlain(HttpServletRequest request) throws IOException{
 
		BufferedReader reader = request.getReader();
        char[] buf = new char[512];
        int len = 0;
        StringBuffer contentBuffer = new StringBuffer();
        while ((len = reader.read(buf)) != -1) {
            contentBuffer.append(buf, 0, len);
        }
        String content= contentBuffer.toString();
        return content;
	}
//在controll中進行呼叫;
String content = HttpJsonUtils.getPostByTextPlain(request);
//根據json資料中物件的key值及其型別取出相應的引數值
JSONObject jsObject = JSONObject.fromObject(content);
			try {
				usermob = jsObject.getString("usermob");
				cpid = jsObject.getString("cpid");
				spid = jsObject.getString("spid");
				type = jsObject.getString("type");
				ordertime = jsObject.getString("ordertime");
				canceltime = jsObject.getString("canceltime");
				endtime = jsObject.getString("endtime");
				channelcode = jsObject.getString("channelcode");
				province = jsObject.getString("province");
				area = jsObject.getString("area");
				ordertype = jsObject.getString("ordertype");
				videoid = jsObject.getString("videoid");
			} catch (Exception e) {
				LOGGER.info("發生錯誤*****" + e.getMessage());
			}