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

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)

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.