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 "";
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM