Android用戶端使用okhttp上傳檔案php伺服器

來源:互聯網
上載者:User
為了更好的在應對網路訪問,最近學習了一下okhttp,感覺挺方便使用的,首先要使用okhttp,需要在項目中匯入okhttp-x.x.x.jar okhttputils-x_x_x.jar okio-x.x.x.jar 這三個jar包,下面貼出Android端和伺服器端關鍵代碼:

private void uplodeImage(File mFile) {//mFile一個真實存在的圖片檔案if (!mFile.exists())        {            Toast.makeText(MainActivity.this, "檔案不存在,請修改檔案路徑", Toast.LENGTH_SHORT).show();            return;        }OkHttpUtils.post().url(UPLODE_URL)    .addFile("file", mFile.getName(), mFile)    .build()    .execute(new Callback() {@Overridepublic void onError(Call arg0, Exception arg1) {System.out.println("上傳檔案失敗");info.setText("REEOR:upload error!");}@Overridepublic void onResponse(String arg0) {//System.out.println(arg0);//上傳成功返迴文件在伺服器上的唯一路徑,將此路徑儲存到資料庫中info.setText(arg0);//這裡會返回一個包含上傳檔案在伺服器上的唯一路徑,上傳成功後將這個字串儲存到資料庫中去}@Overridepublic String parseNetworkResponse(Response arg0) throws Exception {// TODO Auto-generated method stubreturn arg0.body().string();}}) ;}

伺服器端的代碼:

fileName=$fileName;$this->maxSize=$maxSize;$this->allowMime=$allowMime;$this->allowExt=$allowExt;$this->uploadPath=$uploadPath;$this->imgFlag=$imgFlag;$this->fileInfo=$_FILES[$this->fileName];}/** * 檢測上傳檔案是否出錯 * @return boolean */protected function checkError(){if(!is_null($this->fileInfo)){if($this->fileInfo['error']>0){switch($this->fileInfo['error']){case 1:$this->error='超過了PHP設定檔中upload_max_filesize選項的值';break;case 2:$this->error='超過了表單中MAX_FILE_SIZE設定的值';break;case 3:$this->error='檔案部分被上傳';break;case 4:$this->error='沒有選擇上傳檔案';break;case 6:$this->error='沒有找到臨時目錄';break;case 7:$this->error='檔案不可寫';break;case 8:$this->error='由於PHP的擴充程式中斷檔案上傳';break;}return false;}else{return true;}}else{$this->error='檔案上傳出錯';return false;}}/** * 檢測上傳檔案的大小 * @return boolean */protected function checkSize(){if($this->fileInfo['size']>$this->maxSize){$this->error='上傳檔案過大';return false;}return true;}/** * 檢測副檔名 * @return boolean */protected function checkExt(){$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));if(!in_array($this->ext,$this->allowExt)){$this->error='不允許的副檔名';return false;}return true;}/** * 檢測檔案的類型 * @return boolean */protected function checkMime(){if(!in_array($this->fileInfo['type'],$this->allowMime)){$this->error='不允許的檔案類型';return false;}return true;}/** * 檢測是否是真實圖片 * @return boolean */protected function checkTrueImg(){if($this->imgFlag){if(!@getimagesize($this->fileInfo['tmp_name'])){$this->error='不是真實圖片';return false;}return true;}}/** * 檢測是否通過HTTP POST方式上傳上來的 * @return boolean */protected function checkHTTPPost(){if(!is_uploaded_file($this->fileInfo['tmp_name'])){$this->error='檔案不是通過HTTP POST方式上傳上來的';return false;}return true;}/** *顯示錯誤  */protected function showError(){return (Array(    'rType'=>-30,    'rMessage'=>$this->error    ));}/** * 檢測目錄不存在則建立 */protected function checkUploadPath(){if(!file_exists($this->uploadPath)){mkdir($this->uploadPath,0777,true);}}/** * 產生唯一字串 * @return string */protected function getUniName(){return md5(uniqid(microtime(true),true));}/** * 上傳檔案 * @return string */public function uploadFile(){if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){$this->checkUploadPath();$this->uniName=$this->getUniName();$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){return  Array(    'rType' => 0,    'rMessage' => $this->destination    );}else{$this->error='檔案移動失敗';$this->showError();}}else{return $this->showError();}}}

uploadFile(),JSON_UNESCAPED_UNICODE);


以上就介紹了Android用戶端使用okhttp上傳檔案php伺服器,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.