是否可以在POJO类中发送Multipart Image?

时间:2018-11-18 18:06:18

标签: spring retrofit

我当前正在使用Retrofit将具有String Id的图像文件发送到服务器。我想使用POJO类发送带有字符串ID的Multipart Image。有可能吗?

我的POJO类->

public class ImageRequest {

    private String ImageId;
    private MultipartBody.Part Image;

    public MultipartBody.Part getImage() {
        return Image;
    }

    public void setImage(MultipartBody.Part Image) {
        this.Image = Image;
    }

    public String getId() {
        return Id;
    }

    public void setId(String cropId) {
        this.Id = Id;
    }
}

我的上传功能->

private void UploadFiles() {
        File uploadFile = fileArrayList.get(0);
        if (uploadFile != null) {
            Log.d(TAG, "UploadFiles: File Name is -> " + uploadFile.getName());

            // Create a request body with file and image media type
            RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), uploadFile);

            // Create MultipartBody.Part using file request-body,file name and part name
            MultipartBody.Part Image = MultipartBody.Part.createFormData("upload", uploadFile.getName(), fileReqBody);

            //  Id is the name of the Parent Folder...
            String Id = uploadFile.getParentFile().getName();

            ImageRequest.setImage(Image);
            ImageRequest.setId(Id);

            Api.uploadImage(ImageRequest, new Callback<BasicResponse>() {
                @Override
                public void onResponse(Call<BasicResponse> call, Response<BasicResponse> response) {
                    if(response.body()!=null){
                        Log.d(TAG, "onResponse: Success" + response.body().getResponse().toString());
                    }
                }

                @Override
                public void onFailure(Call<BasicResponse> call, Throwable t) {
                    Log.d(TAG, "onResponse: Failure");
                }
            });
        }
    }

我的UploadImage函数->

public static void uploadImage(ImageRequest imageRequest, Callback<BasicResponse> callback) {
        UploadImageApi uploadImageApi = retrofit.create(UploadImageApi.class);
        Call<BasicResponse> call = uploadImageApi.uploadImageFun(imageRequest);
        call.enqueue(callback);
    }

这是我的上传图片API接口->

public interface UploadImageApi {
        @POST(UPLOAD_FILE_URL)
        Call<BasicResponse> uploadImageFun(@Body ImageRequest imageRequest);
    }

在春季,我正在获取ImageRequest类并将其映射到ImageRequest POJO(与Android相同)。

    @RequestMapping(value = "/UploadImage",method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
        @ResponseBody
        public BasicResponse UploadImage(@RequestBody CropImageRequest cropImageRequest) {
                basicResponse.setResponse("HEllo there Response is successful");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Exception Occured...");
            }
            return basicResponse;
}

我无法在Spring端获取图像,谁能告诉我我的错误,我是Spring和Retrofit的新手。谢谢。

0 个答案:

没有答案