Android 上傳圖片並添加參數 PHP接收

來源:互聯網
上載者:User

標籤:

php端接收代碼:

    public function get_file(){        $local_path  = "./Public/daixu_picture/figure/";//伺服器檔案的存放路徑        $img_name = basename( $_FILES[‘uploadedfile‘][‘name‘]);//伺服器中的圖片名(uploadedfile是索引值名,可自行設定)        $target_path = $local_path.$img_name;        $result = move_uploaded_file($_FILES[‘uploadedfile‘][‘tmp_name‘], $target_path);        if($result) {            echo "上傳成功";        } else{            echo "上傳失敗";        }    }

 

android端代碼:

/*    * 作用:上傳圖片,並攜帶參數    * 傳入參數:http_url(伺服器目標地址),filepath(本機圖片的地址)    */    public void uploadImage(final String http_url, final String filepath){        new Thread(new Runnable() {            @Override            public void run() {                try {                    File file = new File(filepath);                    if (!file.exists()) {                        Log.i("錯誤", "檔案不存在");                    }                    HttpClient client = new DefaultHttpClient();                    HttpPost post = new HttpPost(http_url);                    FileBody fileBody = new FileBody(file, "image/jpeg");                    MultipartEntity entity = new MultipartEntity();                    entity.addPart("uploadedfile", fileBody);//uploadedfile是圖片上傳的索引值名                    entity.addPart("key_app", new StringBody("1"));//設定要傳入的參數,key_app是索引值名                    post.setEntity(entity);                    HttpResponse response = client.execute(post);                    if (response.getStatusLine().getStatusCode() == 200) {                        HttpEntity httpEntity = response.getEntity();                        String result = EntityUtils.toString(httpEntity, "utf-8");                        Log.e("返回的結果",result);                    }                }catch (Exception e) {                    e.printStackTrace();                }            }        }).start();    }

 

Android 上傳圖片並添加參數 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.