Principle
The Android client simulates an HTTP POST request to the server side, and when the server receives the corresponding POST request, it returns the response information to the client.
Background
On the Internet a lot of uploaded to the Java server, looking for a long time, found the upload to PHP, the idea and I thought the same, is post the past. Nonsense is not much to say, directly on the map to see the code.
PHP code
<?php
$target _path = "./upload/";//Receive file directory
$target _path = $target _path. basename ($_files[' uploadedfile '] [ ' name ']);
if (Move_uploaded_file ($_files[' uploadedfile '] [' tmp_name '], $target _path)) {
echo "the file". basename ($_files[' uploadedfile '] [' name ']). "has been uploaded";
} else{
echo "There was a error uploading the file, please try again!". $_files[' uploadedfile ' [' Error '];
}
?>
Android Code
Main code to upload:
private void UploadFile (String uploadurl) {string end = ' \ r \ n ';
String Twohyphens = "--";
String boundary = "Hu Jintao";
try {URL url = new URL (uploadurl); HttpURLConnection httpurlconnection = (httpurlconnection) URL. OpenConnection ();//http connect//Set the stream size for each transfer,
can effectively prevent cell phone crashes because of insufficient memory//This method is used to enable a stream of HTTP request bodies that do not have an internal buffer when the content length is not known beforehand.
Httpurlconnection.setchunkedstreamingmode (128 * 1024);//128K//Allow input output stream httpurlconnection.setdoinput (true);
Httpurlconnection.setdooutput (TRUE);
Httpurlconnection.setusecaches (FALSE);
Use the Post method Httpurlconnection.setrequestmethod ("post"); Httpurlconnection.setrequestproperty ("Connection", "keep-alive");/Keep Connected Httpurlconnection.setrequestproperty (" Charset "," UTF-8 ")//Encoding Httpurlconnection.setrequestproperty (" Content-type "," multipart/form-data;boundary= "+
boundary);//post pass past encoding dataoutputstream dos = new DataOutputStream (Httpurlconnection.getoutputstream ());//output stream
Dos.writebytes (twohyphens + boundary + end); Dos.writebytes ("Content-disposition:form-data; Name=\ "UploAdedfile\ ";
Filename=\ "" + srcpath.substring (Srcpath.lastindexof ("/") + 1) + "\" + end);
Dos.writebytes (end); FileInputStream fis = new FileInputStream (srcpath);//File input stream, written to in memory byte[] buffer = new byte[8192];
8k int count = 0;
Read file while ((count = fis.read (buffer))!=-1) {dos.write (buffer, 0, count);} fis.close ();
Dos.writebytes (end);
Dos.writebytes (twohyphens + boundary + twohyphens + end);
Dos.flush (); InputStream is = Httpurlconnection.getinputstream ();//http input, that is, the result returned inputstreamreader ISR = new InputStreamReader (IS
, "Utf-8");
BufferedReader br = new BufferedReader (ISR);
String result = Br.readline ();
Toast.maketext (this, results, Toast.length_long). Show ()//Output The result dos.close ();
Is.close (); catch (Exception e) {e.printstacktrace (); Settitle (E.getmessage ());}}
Since Android 4.0 time-consuming operation requirements are in the non-UI thread operation, the front of the asynctask to use the bar ~
Asynctask Transmission door: http://www.jb51.net/article/77155.htm
In this class, will upload the operation in Doinbackground, can have progressdialog show how many upload:
Read file
bytesread = fileinputstream.read (buffer, 0, buffersize);
while (Bytesread > 0) {
outputstream.write (buffer, 0, buffersize);
length + = buffersize;
progress = (int) ((length *)/totalsize);
Publishprogress (progress);
bytesavailable = Fileinputstream.available ();
buffersize = Math.min (bytesavailable, MaxBufferSize);
Bytesread = fileinputstream.read (buffer, 0, buffersize);
}
Outputstream.writebytes (lineend);
Outputstream.writebytes (twohyphens + boundary + twohyphens
+ lineend);
Publishprogress (100);
There is, attention to the permissions yo:
<uses-permission android:name= "Android.permission.INTERNET"/>
The above content to introduce you to the Android asynchronous upload pictures to the PHP server, I hope this article to share can bring help to everyone.