post方式发送的JSON数据需要以流的方式读取,具体有两种方式。
第一种:以字节方式读取
this.setRequest(ServletActionContext.getRequest());
request.setCharacterEncoding("UTF-8");
ServletInputStream input = request.getInputStream();
int len = request.getContentLength();
if (len <= 0) {
return "";
}
byte[] buffer = new byte[len];
input.read(buffer);
String message = new String(buffer,"UTF-8");
第二种:使用BufferedReader读取,有可能会出现中文乱码问题
this.setRequest(ServletActionContext.getRequest());
request.setCharacterEncoding("UTF-8");
ServletInputStream input = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
try{
while((line = reader.readLine())!=null){
sb.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
//input.close();
}
String message = sb.toString();