Android HTTP uploads files and submits parameters through post (via Assembly Protocol)

Source: Internet
Author: User

Httpurlconnection conn = NULL;

Dataoutputstream outstream = NULL;
Try {
String boundary = "--------------------------- 7da1_7580612"; // data demarcation line

String multipart_form_data = "multipart/form-Data ";

URL url = new URL (Const. url );

// Upload the Form file
Stringbuilder emailsb = new stringbuilder ();
Emailsb. append ("--");
Emailsb. append (Boundary );
Emailsb. append ("\ r \ n ");
Emailsb. append ("content-Disposition: Form-data; name = \" email \ "\ r \ n ");
Emailsb. append (email );
Emailsb. append ("\ r \ n ");

Stringbuilder bitmapsb = new stringbuilder ();
Bitmapsb. append ("--");
Bitmapsb. append (Boundary );
Bitmapsb. append ("\ r \ n ");
Bitmapsb. append ("content-Disposition: Form-data; name = \" image \ "; filename = \" image \ "\ r \ n ");
Bitmapsb. append ("Content-Type: image/PNG \ r \ n ");

Byte [] end_data = ("--" + boundary + "-- \ r \ n"). getbytes (); // data end mark

File file = new file ("/mnt/sdcard/111.png ");
Fileinputstream fileis = new fileinputstream (File );

Long contentlenght = emailsb. tostring (). getbytes (). Length + end_data.length

+ Bitmapsb. tostring (). getbytes (). Length + file. Length () + "\ r \ n". getbytes (). length;

Conn = (httpurlconnection) URL. openconnection ();
Conn. setdoinput (true); // allow input
Conn. setdooutput (true); // allow output
Conn. setusecaches (false); // do not use caches
Conn. setrequestmethod ("Post ");
Conn. setrequestproperty ("connection", "keep-alive ");
Conn. setrequestproperty ("Content-Type", multipart_form_data + "; boundary =" + boundary );
Conn. setrequestproperty ("Content-Length", long. tostring (contentlenght ));


Outstream = new dataoutputstream (conn. getoutputstream ());

Outstream. Write (emailsb. tostring (). getbytes ());

Outstream. Write (bitmapsb. tostring (). getbytes ());

Byte [] buffer = new byte [1024];
Int Len = 0;
While (LEN = fileis. Read (buffer ))! =-1 ){

Outstream. Write (buffer, 0, Len );
}
Outstream. Write ("\ r \ n". getbytes ());


Outstream. Write (end_data );
Outstream. Flush ();
Int CAH = conn. getresponsecode ();
If (CAH! = 200 ){

System. Out. println ("Upload Failed ");

Return;
}
Inputstream is = conn. getinputstream ();
Int ch;
Stringbuilder result = new stringbuilder ();
While (CH = is. Read ())! =-1 ){

Result. append (char) CH );
}
System. Out. println ("Result:" + result. tostring ());
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Finally {

Try {

If (outstream! = NULL ){

Outstream. Close ();

}
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

If (Conn! = NULL ){

Conn. Disconnect ();

Conn = NULL;

}

}

It is rarely used to upload files using the httpurlconnection post form, which is troublesome.

 

The principle is: analyze the data format uploaded by the file, and then construct the corresponding string sent to the server according to the format.

The format is as follows: httppost123 is a self-constructed string, which can be any other string.

---------- Httppost123 (\ r \ n)
Content-Disposition: Form-data; name = "IMG"; filename = "t.txt" (\ r \ n)
Content-Type: Application/octet-stream (\ r \ n)

(\ R \ n)

Sdfsdfsdfsdfsdf (\ r \ n)
---------- Httppost123 (\ r \ n)
Content-Disposition: Form-data; name = "text" (\ r \ n)

(\ R \ n)

Text tttt (\ r \ n)
---------- Httppost123 -- (\ r \ n)
(\ R \ n)

 

The above (\ r \ n) indicates that each data must end with (\ r \ n.

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.