改造将多个图像上传到一个按键

编程入门 行业动态 更新时间:2024-10-07 00:22:32
本文介绍了改造将多个图像上传到一个按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Retrofit将图像上传到我的服务器.在这里,我需要为一个键上传多个图像.我已经尝试过使用Postman网络客户端,它运行良好.这是屏幕截图.

I am using Retrofit to upload images to my server. Here I need to upload multiple images for a single key. I have tried with Postman web client it is working well. Here is a screenshot.

以下是请求的键值对. SurveyImage:[file1,file2,file3]; PropertyImage:文件 DRA:jsonBody

Here are the key value pairs for the request. SurveyImage : [file1,file2,file3]; PropertyImage : file DRA : jsonBody

我尝试对Retrofit做同样的事情.但图像未上传到服务器.这是我的代码. WebServicesAPI.java

I tried to do the same with Retrofit. but the images are not uploading to the server.Here is my code. 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"); }

请帮助我.

推荐答案

我们可以使用MultipartBody.Part数组将图像数组上传到单个键. 这是解决方案 WebServicesAPI

We can use MultipartBody.Part array to upload an array of images to a single key. Here is the solution 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); }

更多推荐

改造将多个图像上传到一个按键

本文发布于:2023-11-26 23:09:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   按键   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!