Android upload file "Content-type", for "Application/octet-stream" with PHP program on the server with $globals[' Http_raw_post_data ') accept (ii)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.