首页 > 代码库 > android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)

android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)

服务端php程序file_up.php

function uploadFileBinary()        {            $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存在是接着上次上传                    $fid = $this->fid;                else //fid不存在,做为第一次上传,生成一个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;  //返回fid  给服务器                $this->saveLog("$fid 上传成功");            }else            {                echo "fail";                $this->saveLog(" 上传失败");            }        }

 

客户端java 代码 

private String fidString = "test01.mp4";    public void doUpload()    {        //要上传的文件         String pathString = FileManager.getParentDirectory()+"media/video_3_20141222145045024.mp4"; //video_3_20141222145045024.mp4  video_3_20141224153340976.mp4        //上传的地址        String acceptUrl = "http://10.0.10.3/flyguard/mobileapi/file_up.php?fid="+this.fidString+"&pos=&ext=mp4";                RandomAccessFile raf =  null;        try        {            raf = new RandomAccessFile(pathString, "r");                        long alllength=raf.length();            raf.seek(0);   //指针编移量,断点续传用到            byte[] buffer = new byte[128*1024];//128k            int count = 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 block                    e.printStackTrace();                }        }                    }            /*     * 提交 的url   Url     * 上传 数据             data     * */    public String PostFileData(String Url,byte[] data)     {      try        {                 HttpURLConnection conn = (HttpURLConnection) new URL(Url).openConnection();            conn.setConnectTimeout(20 * 1000);            conn.setRequestMethod("POST");            conn.setDoOutput(true);// 允许对外输出数据            conn.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 = new BufferedReader(new InputStreamReader(conn.getInputStream()));                String line;                while ((line = reader.readLine()) != null)                 {                    Response += line;                 }            }                        return Response;        }      catch (Exception e)        {            Log.e("PostFileData", e.getMessage());            FileManager.saveError("PostFileData", e);        }      finally        {             return "";        }    }

 

android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)