获取前端post方式传过来的JSON格式的数据的代码

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/koushr/article/details/51124626
public JSONObject getRequestContent(HttpServletRequest req) {
		JSONObject data = null;
		try {
			InputStream is = req.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			String line = null;
			StringBuffer content = new StringBuffer();
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				content.append(line);
				System.out.println(content);
			}
			String reqStr = content.toString().trim();
			if (StringUtils.isEmpty(reqStr)) {
				return new JSONObject();
			}
			if (reqStr.contains("=")) {
				reqStr = reqStr.replaceAll("=", ":");
			}
			if (!reqStr.startsWith("{")) {
				reqStr = "{" + reqStr;
			}
			if (!reqStr.endsWith("}")) {
				reqStr = reqStr + "}";
			}
			data = JSONObject.fromObject(reqStr);
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONObject();
		}
		return data;
	}
展开阅读全文

没有更多推荐了,返回首页