2017-06-01 46 views
1

我需要上傳服務器上的圖像數組使用多部分。我也想給該數組的密鑰,否則服務器無法識別數組。我曾嘗試解決以下,但這些不工作: 解決方法1上傳圖像的多部分改造2

MultipartBody.Part[] array = new MultipartBody.Part[items.size()]; 
      for(int i=0;i<items.size();i++){ 
       File file = new File(items.get(i)); 
       RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file); 
       MultipartBody.Part filePart = MultipartBody.Part.createFormData("value_" 
           + String.valueOf(uploadMissionRequestModel.getStepId()), file.getName(), 
         requestBody); 
       array[i]=filePart; 
      } 

RequestBody apiKey = RequestBody.create(MediaType.parse("text/plain"), 
       uploadMissionRequestModel.getApiKey()); 
     RequestBody stepId = RequestBody.create(MediaType.parse("text/plain"), 
       String.valueOf(uploadMissionRequestModel.getStepId())); 
     RequestBody missionId = RequestBody.create(MediaType.parse("text/plain"), 
       String.valueOf(uploadMissionRequestModel.getMissionId())); 
     RequestBody overWrite = RequestBody.create(MediaType.parse("text/plain"), 
       "yes"); 

     Map<String, RequestBody> partMap = new HashMap<>(); 
     partMap.put("api_token", apiKey); 
     partMap.put("MissionID", missionId); 
     partMap.put("overwrite", overWrite); 
     partMap.put("StepID", stepId); 

ApiServices service = RestClient.getClient(); 
     final Call<UploadMissionResponse> call = service.uploadFiles(partMap, array); 

溶液2

RequestBody[] image_id = new RequestBody[items.size()]; 
     for(int i=0;i<items.size();i++){ 
      File file = new File(items.get(i)); 
      RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file); 
      image_id[i]=requestBody; 

     } 
RequestBody apiKey = RequestBody.create(MediaType.parse("text/plain"), 
       uploadMissionRequestModel.getApiKey()); 
     RequestBody stepId = RequestBody.create(MediaType.parse("text/plain"), 
       String.valueOf(uploadMissionRequestModel.getStepId())); 
     RequestBody missionId = RequestBody.create(MediaType.parse("text/plain"), 
       String.valueOf(uploadMissionRequestModel.getMissionId())); 
     RequestBody overWrite = RequestBody.create(MediaType.parse("text/plain"), 
       "yes"); 

     Map<String, RequestBody> partMap = new HashMap<>(); 
     partMap.put("api_token", apiKey); 
     partMap.put("MissionID", missionId); 
     partMap.put("overwrite", overWrite); 
     partMap.put("StepID", stepId); 

     Map<String, RequestBody[]> imageMap = new HashMap<>(); 
     imageMap.put("value_8",image_id); 

ApiServices service = RestClient.getClient(); 
     final Call<UploadMissionResponse> call = service.uploadFiles(partMap, imageMap); 

請建議,如何傳遞圖像陣列使用retrofit2鍵名。

回答

0

如果我是你,我會將圖像轉換爲Base64,並使用第二種解決方案將List<String>發送給圖像。

1

我的請求方法:

@Multipart 
@POST("admin/assets/upload/") 
Observable<UploadResponse> uploadFiles(@Part MultipartBody.Part... files); 

從URI的陣列創建零件。所有部件將有重點 '的文件[]',一個我認識到後端:

//UriHandle and OkioUtil are my own classes to handle both 'content' and 'file' uris 
//files has type of Uri[] 
final MultipartBody.Part[] parts = new MultipartBody.Part[files.length]; 
for (int i = 0; i < files.length; i++) { 
    final UriHandle handle = UriUtil.getUriHandle(context, files[i]); 
    final RequestBody body = OkioUtil.create(handle, mediaType); 
    parts[i] = MultipartBody.Part.createFormData("files[]", handle.name(), body); 
} 

發送請求:

api.uploadFiles(part) 
    .subscribeOn(...) 
    .observeOn(...) 
    .subscribe(result -> ...); 
+0

非常感謝你。你拯救了我的一天。 – oalpayli