简体   繁体   中英

Why the String inside Controller not printing anything?

I am trying to send Image and Id using retrofit for that i am sending Multipart file and String.

This is my Upload Method on Android side ->

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


            // Parsing any Media type file
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), uploadFile);

            // MultipartBody.Part is used to send also the actual file name
            MultipartBody.Part cropImage = MultipartBody.Part.createFormData("cropImage", uploadFile.getName(), requestFile);

            RequestBody cropId = RequestBody.create(MediaType.parse("multipart/form-data"), uploadFile.getParentFile().getName());

            Api.uploadCropImage(cropImage,cropId, 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());
                    }
                    else{
                        Log.d(TAG, "onResponse: null Response");
                    }
                }

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

My Upload CropImage Method ->

public static void uploadCropImage(MultipartBody.Part multipartBody,RequestBody cropId,
                                                                            Callback<BasicResponse> callback) {
        UploadCropImageApi uploadCropImageApi = retrofit.create(UploadCropImageApi.class);
        Call<BasicResponse> call = uploadCropImageApi.uploadCropImage(multipartBody,cropId);
        call.enqueue(callback);
    }

My Interface ->

 public interface UploadCropImageApi {
        @Multipart
        @POST(UPLOAD_FILE_TO_AWS_URL)
        Call<BasicResponse> uploadCropImage(@Part MultipartBody.Part cropImage, @Part("cropId") RequestBody cropId);
    }

This is my Spring Controller, What's wrong with it? It's not printing cropId.

@RequestMapping(value = "/UploadCropImage", method = RequestMethod.POST, consumes = {"multipart/form-data"})
@ResponseBody
public String UploadImage(@RequestBody MultipartFile cropImage,@RequestBody String cropId ,HttpServletRequest request) {
    System.out.println("String is -> " + cropId);
    return null;
}

You cannot use two @RequestBody as it can bind to a single object only (the body can be consumed only once)

You need to use @RequestParam String cropId instead of RequestBody.

See here for clarification

UPDATE :Here is your controller method look like

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(@RequestParam("name") String name, @RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) { 
    if (!file.isEmpty()) { 
        try { 
            byte[] bytes = file.getBytes();     
            // Creating the directory to store file 
            String rootPath = System.getProperty("catalina.home"); 
            File dir = new File(rootPath + File.separator + "tmpFiles"); 
            if (!dir.exists()) 
                dir.mkdirs();     
            // Create the file on server 
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name); 
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); 
            stream.write(bytes);
            stream.close(); 
            System.out.println("Server File Location=" + serverFile.getAbsolutePath());
            return null; 
        } catch (Exception e) { 
            return null; 
        } 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM