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.