Objective
Recently write an interface, write good later want to test, write Ajax (POST method) to call the interface is can use the action of the Class property of the Get/set method to obtain data. But not just the Ajax of the page will call this interface, and the external system will be called, so I write the normal Java code (not Web project) to call, then I am blind, because I can not receive the parameters Ah!! "What if I can't connect?" I found some details about the various reasons for the search and the other code in the project. The rest of the code in the project uses the HttpServletRequest instance to get the information content using Getparametermap (), but I'm using the getInputStream (), What's the mystery?
"Acquired knowledge"
In fact, before a lot of confused time, only finally know what knowledge should be checked, the middle of the twists and turns because there is no learning what is not nonsense. Straight to the subject.
Search the two method name, see csdn above a post someone asked this question, answer there is a clearer (I copied directly here):
According to the servlet specification, form data in the request body (Entity) will be populated in the parameter collection of requests if the following conditions are met (the Request.getparameter series method can read the relevant data):
1 This is a HTTP/HTTPS request
2 The Request method is post (querystring whether or not post will be set to parameter)
3 Type of request (Content-type header) is application/x-www-form-urlencoded
4 Servlet calls the GetParameter series method
If the above conditions are not met at the same time, the related form data will not be set into the parameter collection of request, and the related data can be accessed through Request.getinputstream (). Conversely, if the above conditions are met, the related form data will no longer be read by Request.getinputstream ().
Servlet Specifiaction 3.0:3.1.1When Parameters is availablethe following is the conditions that Mustbe met before post form data would bepopulated To the parameter set:1. The request is an HTTP or HTTPS request.2. The HTTP method is POST.3. The content type is application/x-www-form-urlencoded.4. The servlet has made a initial call of any of the getparameterfamily of Methodson the request object. If the conditions is not met and the post form data is not included in the Parameterset, the post data must still be Avai Lable to the servlet via the Request object ' s InputStream. If the conditions is met, post form data would no longer be available forreading directly from the Request object ' s input stream.
Similar examples, as well as Response.getoutputstream and getwriter, are often a pair of contradictory bodies.
From this it can be seen that four conditions are indispensable to use the Getparametermap () method to obtain the content, if there is a not satisfied, you can not use this method.
Then I checked the "type of request Content-type" is something. To find a post and a lot of ha, but probably learned that some are:
The role of enctype=multipart/form-data in form
The meaning of enctype= "Multipart/form-data" in the form is to set the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded and cannot be used for file uploads;
Only use the Multipart/form-data to complete the transfer of file data, the following operation.
In the syntax of the form element, Enctype indicates the format of the submitted data, using the Enctype property to specify the type of encoding the browser uses when sending data back to the server.
Below is the description:
(1) application/x-www-form-urlencoded: The form data is encoded as a name/value pair. This is the standard encoding format, and the specific data examples are shown below.
Custname= Surin &elecontid=elecontid0001&idtype=0&idno =411303198802190512 &custage=30&education=20
(2) Multipart/form-data: The form data is encoded as a message, and each control on the page corresponds to a part of the message.
(3) Text/plain: The form data is encoded in plain text, with no controls or formatting characters.
Enctype= "Multipart/form-data" is used to upload images in the form.
<form name= "UserInfo" method= "post" action= "first_submit.jsp" enctype= "Multipart/form-data" >
Set enctype= "Multipart/form-data" in the Form tab to ensure the correct encoding of the anonymous upload file.
As follows
<TR><TDHeight= "+"Align= "Right">Upload Enterprise business License Image:</TD><TD><INPUTTYPE= "FILE"NAME= "UploadFile"SIZE= " the"OnChange= "Checkimage ()"></TD></TR>
You have to add Enctype= "Multipart/form-data".
The meaning of enctype= "Multipart/form-data" in the form is to set the MIME encoding of the form. By default,
This encoding format is application/x-www-form-urlencoded and cannot be used for file upload;
Only use the Multipart/form-data to complete the transfer of file data, the following operation.
Enctype= "Multipart/form-data" is to upload binary data; The value of input in the form is passed in 2 binary ways.
The value of input in the form is passed through in 2, so the request is not valued.
That is, add this code, with the request will be passed unsuccessful, take the form value added to the database, use the following:
Smartupload su = new smartupload ();//Create a new Smartupload object Su.getrequest (). Getparametervalues (); Take the array value su.getrequest (). GetParameter (); Take a single parameter single value
Later found that if it refers to the content of the upload data, for different files, there are many kinds of files, will correspond to a variety of different types. Only the request default is application/x-www-form-urlencoded, so with Getparametermap () can be obtained, with getinputstream () cannot get.
"The problem is not solved"
In fact, there is a problem with the package of the project httpclient data transmitted, is able to get the way to receive streams, but I do not understand how httpclient works ... Still have to learn.
"Original" http-data for POST request with getInputStream () or Getparametermap ()