Sometimes, the data submitted by some requests is not the common parameter name: the key-value pair of the parameter value mapping relationship. For example, the data submitted by the WeChat public platform server to the developer's specified URL is an xml string. At this time, it cannot be passed It cannot be obtained through java's request.getParameter("parameter name"), nor through php's $_POST['parameter name']. For this kind of data, the solution is as follows:
request.setCharacterEncoding("utf-8"); StringBuilder buffer = new StringBuilder(); java.io.BufferedReader reader=null; try{ /** * getReader() * Retrieves the body of the request as character data using a BufferedReader * getInputStream() * Retrieves the body of the request as binary data using a ServletInputStream. */ reader = request.getReader(); String line=null; while((line = reader.readLine())!=null){ buffer.append(line); } }catch(java.io.IOException e){ e.printStackTrace(); }finally{ if(null!=reader){ try { reader.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } } String res = buffer.toString(); System.out.print(res);
Additional explanation: getReader() and getInputStream () can only be called once once requested, and both cannot be called at the same time.
Php code
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
Additional explanation:
html from enctype attribute specifies how to encode form data before sending it to the server. The default default value is "application/x-www-form-urlencoded" .
application/x-www-form-urlencoded will encode the transmitted data into the form of key-value pairs. The backend can directly obtain
text/plain through request.getParameter(), and the data will be encoded in plain text, where Does not contain any controls or formatting characters.
multipart/form-data, the data transmitted must use the multimedia transmission protocol. Since multimedia transmits a large amount of data, it is stipulated that the uploaded file must be the post method.
When uploading files, the encoding type used should be "multipart/form-data", which can send text data and also supports binary data upload.