如何在Android中使用Multiparty Entity上传多个图像

时间:2015-11-28 09:31:28

标签: php android android-asynctask multipartentity

我必须从我的Android设备上传多个图像,但我坚持认为我如何发送图像数组。

所以我最终决定把我的PHP代码:

  $id=$_REQUEST['id'];

$tot_img=count($_FILES['uploads']['name']);
$of_img='';
for($i=0;$i<$tot_img;$i++)
{
    if($_FILES['uploads']['name'][$i]!="")
    {
        $path=$_SERVER['DOCUMENT_ROOT']."/uploads/offer_img/";
        $logo_name=$_FILES['uploads']['name'][$i];
        list($name,$ext)=explode('.',$_FILES['uploads']['name'][$i]);
        $logo_name=$name.$i.date('His').'.'.$ext;
        $path=$path.$logo_name;
        move_uploaded_file($_FILES['uploads']['tmp_name'][$i],$path);
        if($of_img!='')
        {
            $of_img=$of_img.','.$logo_name;
        }
        else
        {
            $of_img=$logo_name;
        }

    }
}

所以请告诉我如何发送图片。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

我在android中使用以下代码从android

上传多个图像
            private void doFileUpload(List<Uri> mDetials){
                    List<File> mFiles=new ArrayList<>();
                    for(int i=0;i<mDetials.size();i++){
                        String mFileLocation=getPath(getApplicationContext(),mDetials.get(i));
                        File file = new File(mFileLocation);
                        mFiles.add(file);
                    }
                    String urlString = "php_file_link.php";
                    try {
                        HttpClient client = new DefaultHttpClient();
                        HttpPost post = new HttpPost(urlString);
                        List<FileBody> mFileBody=new ArrayList<>();
                        for(int i=0;i<mFiles.size();i++){
                            FileBody bin1 = new FileBody(mFiles.get(i));
                            mFileBody.add(bin1);
                        }
                        MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
                        reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                        for(int i=0;i<  mFileBody.size();i++){
                            reqEntity.addPart("uploadedfile"+i,mFileBody.get(i));
                        }
                        reqEntity.addTextBody("size",""+mDetials.size(), ContentType.TEXT_PLAIN);
                        post.setEntity(reqEntity.build());
                        HttpResponse response = client.execute(post);
                        HttpEntity resEntity = response.getEntity();
                        final String response_str = EntityUtils.toString(resEntity);

                        if (resEntity != null) {
                            Log.i("RESPONSE", response_str);
                            dialog.dismiss();
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    try {
                                        Toast.makeText(getApplicationContext(), response_str, Toast.LENGTH_LONG).show();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }});
                        }
                    } catch (Exception ex){
                        Log.e("Debug", "error: " + ex.getMessage(), ex);
                        dialog.dismiss();
                    }
            }

我使用以下php代码在服务器上上传

$size = $_REQUEST['size'];
for ($x = 0; $x < $size; $x++) {
$target_path="../GizboImages/customer_care/";
$target_path=$target_path.basename( $_FILES["uploadedfile".$x]['name']);
if(move_uploaded_file($_FILES["uploadedfile".$x]['tmp_name'], $target_path)) ;
}