自定义Servlet通过流获取JSON格式的参数,代码如下:
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer("");
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
br.close();
String param = sb.toString();
}
param为带有参数的字符串。然后将字符串转化为JSON对象,JSONObject json = JSONObject.fromObject(param);但是我用postman测试,转JSON对象出错,最后没办法从字符串中截取出了参数。