HTTP protocol Related content

Source: Internet
Author: User

1 Plain Text forms:

1:get request, pass the parameter through the URL plaintext, unsafe and the parameter size is limited. In a servlet, you can get the parameter string using the following code:

    protected void doget (httpservletrequest request, httpservletresponse response)             throws servletexception, IOException {        = request.getquerystring ();        System.out.println (queryString);    }

Print content:

Name=get_name&password=get_password//form has only two properties

2:post request, for uploading data must use POST request, in theory, post is no size limit. The HTTP protocol specification does not have a size limit, and the processing power of the server's handlers is limited. The Post requests the following method to get the request parameters:

    protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//1. Get form Data FlowInputStream in =Request.getinputstream (); //2. Conversion FlowInputStreamReader instream =NewInputStreamReader (In, "UTF-8"); //3. Buffer StreamBufferedReader reader =NewBufferedReader (instream); //Output DataString str =NULL;  while(str = reader.readline ())! =NULL) {System.out.println (str); }        //CloseReader.close ();        Instream.close ();    In.close (); }

The same form prints the following data:

Name=post_name&password=post_password

2 Forms for uploading data:

You must use the Post method, and the form must set the Enctype= "Multipart/form-data" property, for example:

    <DivAlign= "Center">        <formAction= "Fileload"Method= "POST"enctype= "Multipart/form-data">Name:<inputname= "Name"type= "text" /> <BR>Password:<inputtype= "File"name= "UploadFile" /><BR>            <Buttontype= "Submit">Send</Button>        </form>    </Div>

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 multipart/form-data can be used for full file data transfer.

At this point, if the background or the previous post to get data directly using the stream to get data, you will find the uploaded file error, error sample code:

    /*** Upload file corruption *@paramRequest *@paramResponse *@throwsException*/     Public Static voidUploaderror (HttpServletRequest request, httpservletresponse response)throwsException {//Upload error: Failed to filter out some control informationInputStream in =Request.getinputstream ();//This stream is the sum of all the form field data and file databyte[] B =New byte[1024]; FileOutputStream out=NewFileOutputStream (NewFile ("Servlet-fileupload.avi")); intBytesread = 0;  while((Bytesread = In.read (b)) > 0) {//Here we consider all data to be uploading data, resulting in corrupted upload file Out.write (b,0, Bytesread);        Out.flush ();        } out.flush ();        Out.close ();        In.close (); System.out.println (Finished "); }

Remember: The above is the error example!!!

The following is a good example:

    protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Judging is a multimedia content        if(servletfileupload.ismultipartcontent (Request)) {Try{diskfileitemfactory Factory=Newdiskfileitemfactory ();                Factory.setsizethreshold (Memory_threshold); Servletfileupload Upload=Newservletfileupload (Factory); //set maximum file upload valueUpload.setfilesizemax (max_file_size); List<FileItem> items =upload.parserequest (Request);  for(inti = 0, size = items.size (); i < size; i++) {Fileitem Item=Items.get (i); if(!item.isformfield ()) {//Isformfield:true represents a property of the form, false means an upload fileFile File =NewFile ("upload-daxin.flv");//File name can use the actual name of the uploaded file, the example is skippeditem.write (file); }                }            } Catch(Exception e) {e.printstacktrace (); }        }    }

Write relatively simple, always share is the idea. Forgive me!

HTTP protocol Related content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.