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

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

你可能感兴趣的:(获取前端post方式传过来的JSON格式的数据的代码)