android 上传文件用php程序在服务端接受(一)
生活随笔
收集整理的這篇文章主要介紹了
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];//128kint 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-dataDataOutputStream 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 "";}?
轉載于:https://www.cnblogs.com/longhs/p/4184506.html
總結
以上是生活随笔為你收集整理的android 上传文件用php程序在服务端接受(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统下如何禁止ping命令或允
- 下一篇: Java后台请求远程链接