简体   繁体   English

为什么控制器内的字符串不打印任何内容?

[英]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. 我正在尝试使用改造发送图像和ID,因为我正在发送Multipart文件和字符串。

This is my Upload Method on Android side -> 这是我在Android端上传的方法->

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 -> 我的上传CropImage方法->

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? 这是我的Spring Controller,这是怎么回事? It's not printing cropId. 它不打印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) 您不能使用两个@RequestBody,因为它只能绑定到单个对象(主体只能使用一次)

You need to use @RequestParam String cropId instead of RequestBody. 您需要使用@RequestParam String cropId而不是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; 
        } 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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