Android simulates POST requests to upload files

Source: Internet
Author: User

This is made after viewing the online materials and examples in the android SDK development example book. It is basically stuck, but only a few codes are added. The following code is used:

 

Public static string uploadbitmap1 (string urlstring, byte [] imagebytes ){

String endstring = "/R/N ";

String twohyphen = "--";

String boundary = "*****";

Try {

URL url = new URL (urlstring );

Final httpurlconnection con = (httpurlconnection) URL. openconnection ();

// Allow input and output Without Using Cache

Con. setdoinput (true );

Con. setdooutput (true );

Con. setusecaches (false );

// Set the transfer method = post

Con. setrequestmethod ("Post ");

// Setrequestproperty

Con. setrequestproperty ("connection", "keep-alive ");

Con. setrequestproperty ("charset", "UTF-8 ");

Con. setrequestproperty ("Content-Type", "multipart/form-data; boundary =" + boundary );

 

// Timeout value for data reading from the host (unit: milliseconds)

Con. setreadtimeout (50000 );

// Set the timeout time for connecting to the host (unit: milliseconds)

Con. setconnecttimeout (50000 );

 

// System. Out. println (con. getresponsecode ());

// Set dataoutputstream

Dataoutputstream dsdataoutputstream = new dataoutputstream (con. getoutputstream ());

Dsdataoutputstream. writebytes (twohyphen + boundary + endstring );

Dsdataoutputstream. writebytes ("content-Disposition: Form-data;" + "name =/" file1/"; filename =/" "+

"11.jpg/" "+ endstring );

Dsdataoutputstream. writebytes (endstring );

 

Dsdataoutputstream. Write (imagebytes, 0, imagebytes. Length );

Dsdataoutputstream. writebytes (endstring );

Dsdataoutputstream. writebytes (twohyphen + boundary + twohyphen + endstring );

 

Dsdataoutputstream. Close ();

Int CAH = con. getresponsecode ();

If (CAH = 200 ){

Inputstream isinputstream = con. getinputstream ();

Int ch;

Stringbuffer buffer = new stringbuffer ();

While (CH = isinputstream. Read ())! =-1 ){

Buffer. append (char) CH );

}

Return buffer. tostring ();

} Else {

Return "false ";

}

} Catch (exception e ){

E. printstacktrace ();

Return "false ";

}

}

 

 

In the passed parameters, byte [] imagebytes is the byte array obtained after converting the image or the file to be uploaded. If you don't want to explain anything else, check the following articles (a good article found on the Internet ).

Http://zsnlovewl.javaeye.com/blog/636650

Http://student.csdn.net/space.php? Uid = 46868 & Do = Blog & id = 25873

 

Write a simple code for converting bitmap into a byte array:

 

Bytearrayoutputstream OS = new bytearrayoutputstream ();

BM. Compress (compressformat. JPEG, 70, OS );

Byte [] imgbytes = OS. tobytearray ();

BM is a bitmap object.

 

Related Article

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.