将文件数组从android上传到$ _FILES

时间:2015-07-22 02:50:02

标签: android androidhttpclient

我想从我的Android代码上传几个文件,以便它可以在$ _FILES数组中使用。

服务器正在将文件作为数组查找。服务器中的php代码看起来像

for ($i=0;$i<$NumFiles;$i++){
   ...
   $filename = $_FILES["fileset"]["name"][$i];
   $filetype = $_FILES["fileset"]["type"][$i];
   $file = curl_image_create($filePath,$filetype,$filename);
   ...
}

我的文件[](fileSet)中加载了我的android代码

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost request = new HttpPost(this.apiURL);
        MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();
        StringBody numFilesBody = new StringBody("" + fileSet.length, ContentType.TEXT_PLAIN);
        mpEntity.addPart("NumFiles", numFilesBody);
        for (int i = 0; i < fileSet.length; i++) {
            //What should I be doing here to get the $_FILES set up correctly
        }
        request.setEntity(mpEntity.build());
        HttpResponse response = client.execute(request);

我需要设置HTT PRequest,以便服务器可以将$ _Files中的文件作为数组读取。

1 个答案:

答案 0 :(得分:5)

File files [] = .....

然后将它们添加到for循环中:

mpEntity.addPart( "fileset[" + i + "]", new FileBody(files[i]) );