Background
On the Internet many uploaded to the Java server, looking for a long time, found the upload to PHP, thinking and I thought the same, is post past. Don't say much nonsense, just look at the code.
PHP code
PHP code
-
- $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
Upload the main code:
Java code
- private void UploadFile (String uploadurl)
- {
- String end = "\ r \ n";
- String Twohyphens = "--";
- String boundary = "******";
- Try
- {
- URL url = new URL (uploadurl);
- HttpURLConnection httpurlconnection = (httpurlconnection) URL
- . OpenConnection (); HTTP connection
- Set the stream size for each transfer to effectively prevent the phone from crashing due to low memory
- Httpurlconnection.setchunkedstreamingmode (128 * 1024); 128K
- Allow input/output stream
- Httpurlconnection.setdoinput (TRUE);
- Httpurlconnection.setdooutput (TRUE);
- Httpurlconnection.setusecaches (FALSE);
- Using the Post method
- Httpurlconnection.setrequestmethod ("POST");
- Httpurlconnection.setrequestproperty ("Connection", "keep-alive"); Keep connected
- Httpurlconnection.setrequestproperty ("Charset", "UTF-8"); Coding
- Httpurlconnection.setrequestproperty ("Content-type",
- "multipart/form-data;boundary=" + boundary); Post passes past the 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 into 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, which results in the return
- InputStreamReader ISR = new InputStreamReader (IS, "utf-8");
- BufferedReader br = new BufferedReader (ISR);
- String result = Br.readline ();
- Toast.maketext (this, result, Toast.length_long). Show (); Output the result
- Dos.close ();
- Is.close ();
- } catch (Exception e)
- {
- E.printstacktrace ();
- Settitle (E.getmessage ());
- }
- }
Because the time-consuming operations required after Android 4.0 are operating in a non-UI thread, the front asynctask will be used.
Asynctask Portal: http://www.cnblogs.com/yydcdut/p/3707960.html
In this class, the upload operation is placed in the Doinbackground, you can have progressdialog show how much uploaded:
Java code
- 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);
Also, pay attention to the authority yo:
xml/html Code
- < uses-permission Android:name = "Android.permission.INTERNET"/>