我需要使用多部分在服务器上上传一个图像数组。我也想给该数组提供密钥,否则服务器无法识别该数组。我已尝试过以下解决方案,但这些不起作用: 解决方案1 :
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 :(得分:3)
我的请求方法:
@Multipart
@POST("admin/assets/upload/")
Observable<UploadResponse> uploadFiles(@Part MultipartBody.Part... files);
从uri的数组创建部件。所有部分都有关键的'files []',我的后端识别出来的那个:
//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 -> ...);
答案 1 :(得分:0)
如果我是你,我会将图像转换为Base64
并使用第二种解决方案发送List<String>
图像。