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
PrivateString fidstring = "Test01.mp4"; Public voiddoupload () {//the file to uploadString pathstring = filemanager.getparentdirectory () + "Media/video_3_20141222145045024.mp4";//Video_3_20141222145045024.mp4 Video_3_20141224153340976.mp4//Address to uploadString Accepturl = "http://10.0.10.3/flyguard/mobileapi/file_up.php?fid=" + This. fidstring+ "&pos=&ext=mp4"; Randomaccessfile RAF=NULL; Try{RAF=NewRandomaccessfile (pathstring, "R"); LongAlllength=raf.length (); Raf.seek (0);//pointer to the amount of movement, the breakpoint continued to use the byte[] buffer =New byte[128*1024];//128k intCount = 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 ;String Result=Postfiledata (Accepturl,buffer); System.out.println ("Mediaactivity doupload return:" +result+ "Count:" +count); } } Catch(Exception e) {e.printstacktrace (); }finally{ Try { if(raf!=NULL) Raf.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } /** URL URL to submit * Upload data **/ PublicString Postfiledata (String Url,byte[] 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); String Response=""; if(Conn.getresponsecode () = = 200) {BufferedReader reader=NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while(line = Reader.readline ())! =NULL) {Response+=Line ; } } returnResponse; } Catch(Exception e) {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)