前端数据到后端----从request里取数据

1. 参数放在request的header上:request.getParameter()   /   request.getParameter() 

2. 参数放在request的body上:

@RequestMapping(value = "/XXXX", method = RequestMethod.POST, headers = "Content-type=text/plain")

@ResponseBody

public HashMap dataCenterEntry(final HttpServletRequest request){

String body =null;
try {
body = StreamUtils.copyToString(request.getInputStream(), Charset.forName("utf-8"));
} catch (IOException e) {
e.printStackTrace();

}

               return XXXX;

}

期间碰到一个问题,body 始终为空,怎么都接收不到数据;

解决办法:在@RequestMapping(value = "/XXXX", method = RequestMethod.POST)里加个headers = "Content-type=text/plain"后就好了;

---------------------------------------------------------------------------------------------------------

public static String copyToString(InputStream in, Charset charset) throws IOException {
Assert.notNull(in, "No InputStream specified");
StringBuilder out = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, charset);
char[] buffer = new char[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = reader.read(buffer)) != -1) {
out.append(buffer, 0, bytesRead);
}
return out.toString();
}










你可能感兴趣的:(运行过程类)