android 上传文件用php程序在服务端接受(一)

 

php服务端接受程序。。file_up.php。

<?php
/*    require_once(‘lib/session_config.php‘);
    require_once(‘lib/flydc.php‘);
    require_once(‘lib/common.php‘);
    require_once(‘config.php‘); */
    //header("content-Type:text/html;charset=UTF-8");
    
//    var_dump($_SERVER[‘HTTP_RANGE‘]);
    $fileUp  = new FileUp();
    $fileUp->uploadFile();
    exit;
    class FileUp
    {
        public $isHaveFid = false;
        public $target_path = "../files/";  //文件存取的路径
        
        public $uid,$fid,$ext,$alllength,$poss,$finish;
        function initData()
        {
            $this->fid = $_GET[‘fid‘];
            $this->ext = $_GET[‘ext‘];
            $this->poss = $_GET[‘pos‘];
        }
        
        function uploadFile()
        {
            $this->initData();
            
            
            $absoluteName  = "";//$this->getdir()."/".basename($_FILES[‘uploadedfile‘][‘name‘]);
            
            $handleRead = null;
            $fid = "";
            
            $handleWrite = null;
            if(!empty($_FILES[‘uploadedfile‘][‘tmp_name‘]))
            {
                $handleRead = fopen($_FILES[‘uploadedfile‘][‘tmp_name‘],‘rb‘);
                //$extend = pathinfo( $_FILES[$name][‘name‘] );   //$extend[‘extension‘]  扩展
                
                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‘);
                $buffer = ‘‘;
                while (!feof($handleRead)) 
                 {
                    $buffer = fread($handleRead, 1024*128);
                    if(strlen($buffer)<=0)
                        break;
                    fwrite($handleWrite,$buffer);
                     
                }
                
                fclose($handleWrite);
                fclose($handleRead);
                
                
                
                echo $fid;  //返回fid  给服务器
                $this->saveLog("$fid 上传成功");
            }else
            {
                echo "fail";
                $this->saveLog(" 上传失败");
            }
        }
        
        
        
        function saveLog($content)
        {
            $logpath = $this->getdir()."/".date("Y-m-d",time())."_log.txt";
            $result = fopen($logpath,‘a‘);
            fwrite($result,date("Y-m-d H:i:s",time())." ========== ".$content."\r\n");
            fclose($result);
            
            
            
        }
        
        function getdir()
        {
            $day_dir = $this->target_path.date("Ymd",time());
            if(!is_dir($day_dir))
            {
                mkdir($day_dir,0777,true);
            }
            return $day_dir;
        }
        
        
    }


    
    
 
?>

 

 

androiud 客户端java 代码 

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;
            }
            
           
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            
                try
                {
                    if(raf!=null)
                        raf.close();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        
        
    }





public String uploadFil(String acceptUrl,byte[] data)
    {
        
        
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "******";
        try
        {
            URL url = new URL(acceptUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃  
            // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。 
            httpURLConnection.setChunkedStreamingMode(data.length);// 128*1024 是128k
//            允许输入输出流
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            // 使用POST方法 
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);//application/octet-stream   multipart/form-data
            DataOutputStream dos  = new DataOutputStream(httpURLConnection.getOutputStream()); 
            dos.writeBytes(twoHyphens + boundary + end);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
           // +pathString.substring(pathString.lastIndexOf("/")+1)
            +"myfilename"
            +"\""
            +end);
            dos.writeBytes(end);
            
            
            dos.write(data,0,data.length);
            
            dos.writeBytes(end);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
            dos.flush();
            
            
            String reponse = "";
            if(httpURLConnection.getResponseCode() == 200 )
            {
                InputStream is = httpURLConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is,"utf-8");
                BufferedReader br = new BufferedReader(isr);
                while (null !=br.readLine())
                {
                    reponse +=br.readLine(); 
                    
                }
                is.close();
            }
            
            System.out.println("MediaActivity uploadFil Reponse:"+reponse);
            dos.close();
            return reponse;

        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("MediaActivity uploadFil Exception:"+e.getMessage());
        }
        
        return "";
        
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。