第一种方法,获取post请求发送的Json数据
	public static String JsonReq(HttpServletRequest request) {
		BufferedReader br;
		StringBuilder sb = null;
		String reqBody = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					request.getInputStream()));
			String line = null;
			sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			reqBody = URLDecoder.decode(sb.toString(), "UTF-8");
			reqBody = reqBody.substring(reqBody.indexOf("{"));
			request.setAttribute("inputParam", reqBody);
			System.out.println("JsonReq reqBody>>>>>" + reqBody);
			return reqBody;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "jsonerror";
		}
	}

第二种方法,获取post、get请求发送的数据
	public static String getParamString(HttpServletRequest request) {
		Map<String, String[]> map = request.getParameterMap();
		StringBuilder sb = new StringBuilder();
		String reqBody = null;
		try {
			for (Entry<String, String[]> e : map.entrySet()) {
				sb.append(e.getKey()).append("=");
				String[] value = e.getValue();
				if (value != null && value.length == 1) {
					sb.append(value[0]).append("&");
				} else {
					sb.append(Arrays.toString(value)).append("\n");
				}
			}
			reqBody = URLDecoder.decode(sb.toString(), "UTF-8");
			if (reqBody.contains("{") && reqBody.contains("}")) {
				reqBody = reqBody.substring(reqBody.indexOf("{"));
				reqBody = reqBody.substring(0, reqBody.lastIndexOf("}") + 1);
			} else if (reqBody.contains("&")) {
				reqBody = reqBody.substring(0, reqBody.lastIndexOf("&"));
			}
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			System.out.println("JsonReq reqBody>>>>>" + reqBody);
		}
		return reqBody;
	}

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐