我正在使用Retrofit将图像上传到我的服务器。在这里,我需要为单个密钥上传多个图像。我已经尝试使用Postman Web客户端,它运行良好。这是截图。
以下是请求的键值对
SurveyImage:[file1,file2,file3];
PropertyImage:文件
DRA:jsonBody
我试着用Retrofit做同样的事情。但图像没有上传到服务器。这是我的代码 的 WebServicesAPI.java
public interface WebServicesAPI {
@Multipart
@POST(WebServices.UPLOAD_SURVEY)
Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part surveyImage, @Part MultipartBody.Part propertyImage, @Part("DRA") RequestBody dra);
}
以下是上传文件的方法。
private void requestUploadSurvey() {
File propertyImageFile = new File(surveyModel.getPropertyImagePath());
RequestBody propertyImage = RequestBody.create(MediaType.parse("image/*"), propertyImageFile);
MultipartBody.Part propertyImagePart = MultipartBody.Part.createFormData("PropertyImage", propertyImageFile.getName(), propertyImage);
JSONObject requestBody = getRequestBody();
RequestBody draBody = null;
try {
draBody = RequestBody.create(MediaType.parse("text/plain"), requestBody.toString(1));
Log.d(TAG, "requestUploadSurvey: RequestBody : " + requestBody.toString(1));
} catch (JSONException e) {
e.printStackTrace();
}
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
MultipartBody surveyImage = null;
for (SurveyModel.PictureModel model : surveyModel.getPicturesList()) {
File file = new File(model.getImagePath());
builder.addFormDataPart("SurveyImage", file.getName(),
RequestBody.create(MediaType.parse("image/*"), file));
}
surveyImage = builder.build();
final WebServicesAPI webServicesAPI = RetrofitManager.getInstance().getRetrofit().create(WebServicesAPI.class);
Call<UploadSurveyResponseModel> surveyResponse = null;
surveyResponse = webServicesAPI.uploadSurvey(MultipartBody.Part.createFormData("SurveyImage", "SurveyImage", surveyImage), propertyImagePart, draBody);
surveyResponse.enqueue(this);
Log.d(TAG, "requestUploadSurvey: sent the request");
}
请帮我解决这个问题。
答案 0 :(得分:25)
我们可以使用MultipartBody.Part
数组将图像数组上传到单个键。
这是解决方案
的 WebServicesAPI 强>
public interface WebServicesAPI {
@Multipart
@POST(WebServices.UPLOAD_SURVEY)
Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part[] surveyImage, @Part MultipartBody.Part propertyImage, @Part("DRA") RequestBody dra);
}
以下是上传文件的方法。
private void requestUploadSurvey() {
File propertyImageFile = new File(surveyModel.getPropertyImagePath());
RequestBody propertyImage = RequestBody.create(MediaType.parse("image/*"), propertyImageFile);
MultipartBody.Part propertyImagePart = MultipartBody.Part.createFormData("PropertyImage", propertyImageFile.getName(), propertyImage);
MultipartBody.Part[] surveyImagesParts = new MultipartBody.Part[surveyModel.getPicturesList().size()];
for (int index = 0; index < surveyModel.getPicturesList().size(); index++) {
Log.d(TAG, "requestUploadSurvey: survey image " + index + " " + surveyModel.getPicturesList().get(index).getImagePath());
File file = new File(surveyModel.getPicturesList().get(index).getImagePath());
RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"), file);
surveyImagesParts[index] = MultipartBody.Part.createFormData("SurveyImage", file.getName(), surveyBody);
}
final WebServicesAPI webServicesAPI = RetrofitManager.getInstance().getRetrofit().create(WebServicesAPI.class);
Call<UploadSurveyResponseModel> surveyResponse = null;
if (surveyImagesParts != null) {
surveyResponse = webServicesAPI.uploadSurvey(surveyImagesParts, propertyImagePart, draBody);
}
surveyResponse.enqueue(this);
}
答案 1 :(得分:5)
我尝试过的最佳解决方案
<强> ApiInterface:强>
@Multipart
@POST("person/img")
Call<ResponseBody> upImageMany(@Part List<MultipartBody.Part> file);
<强>活动:强>
List<MultipartBody.Part> parts = new ArrayList<>();
for (int i=0; i < upFileList.size(); i++){
parts.add(prepareFilePart("my_file["+i+"]", upFileList.get(i)));
}
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri){
File file = new File(getPath(fileUri));
RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
}
答案 2 :(得分:0)
类createFormData
的第一个参数@method MultipartBody.Part
是一个字符串,它是&#39;键&#39;或输入名称,您可以将字符串images[]
作为数组传递,稍后您可以使用后端语言处理它并循环它以获取位置(i)