Server PHP Program file_up.php
functionuploadfilebinary () {$this-InitData (); $absoluteName= ""; $fid= ""; $handleWrite=NULL; if(!Empty($GLOBALS[' Http_raw_post_data ']) &&strlen($GLOBALS[' Http_raw_post_data ']) >0) { if(!Empty($this->FID))//FID exists is then last uploaded $fid=$this-FID; Else //FID does not exist, as the first upload, generates a FID $fid= Time().‘ _‘.Mt_rand(1,22222). ".".$this-ext; $absoluteName=$this->getdir (). " /".$fid; $handleWrite=fopen($absoluteName, ' a '); fwrite($handleWrite,$GLOBALS[' Http_raw_post_data ']); fclose($handleWrite); Echo $fid;//return FID to the server $this->savelog ("$fidUpload successful "); }Else { Echo"Fail"; $this->savelog ("Upload failed"); } }
Client Java code
Private Stringfidstring = "Test01.mp4"; Publicvoid Doupload () {//the file to upload Stringpathstring = filemanager.getparentdirectory () + "Media/video_3_20141222145045024.mp4";//video_3_20141222145045024.mp4 Video_3_20141224153340976.mp4//uploaded address StringAccepturl = "http://10.0.10.3/flyguard/mobileapi/file_up.php?fid=" +this.fidstring+ "&pos=&ext=mp4"; Randomaccessfile RAF=NULL; Try{RAF=NewRandomaccessfile (pathstring, "R"); Long Alllength=raf.length (); RAF. Seek (0);//pointer to the amount of movement, the breakpoint continued to use thebyte[] Buffer =NewBYTE[128*1024];//128kIntCount= 0; while((Count= raf.read (buffer))! =-1) {//count = raf.read (buffer);//String result = Uploadfil (accepturl,buffer);// System.out.println ("Mediaactivity doupload return:" +result+ "Count:" +count);//break; Stringresult = Postfiledata (Accepturl,buffer); System. OUT.PRINTLN ("Mediaactivity doupload return:" +result+ "Count:" +Count); } } Catch(Exceptione) {e.Printstacktrace (); }finally{Try { if(raf!=NULL) RAF.Close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.Printstacktrace (); } } } /** URL URL to submit * Upload data **/ Public StringPostfiledata (StringUrlbyte[] data) { Try{HttpURLConnection conn= (httpurlconnection)NewURL (URL).OpenConnection (); Conn. setconnecttimeout (20 * 1000); Conn. Setrequestmethod ("POST"); Conn. Setdooutput (true);//allow external output dataConn.setrequestproperty ("Content-type", "Application/octet-stream"); Conn. Setrequestproperty ("Content-length",String. valueOf (data.length)); OutputStream OutStream= Conn.Getoutputstream (); OutStream.write (data); StringResponse= ""; if(Conn.getresponsecode () = = 200) {BufferedReader reader=NewBufferedReader (NewInputStreamReader (Conn.getInputStream ())); StringLine ; while(line = Reader.readline ())! =NULL) {Response+=Line ; } } returnResponse; } Catch(Exceptione) {Log. E ("Postfiledata", E.getMessage ()); FileManager. Saveerror ("Postfiledata",e); } finally {return""; } }
Android upload file "Content-type", for "Application/octet-stream" with PHP program on the server with $globals[' Http_raw_post_data ') accept (ii)