java - 为什么 Controller 内的字符串不打印任何内容?

标签 java android spring-boot retrofit2

我正在尝试使用改造发送图像和 ID,因为我正在发送多部分文件和字符串。

这是我在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");
                }
            });
        }
    }

我的上传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);
    }

我的界面 ->

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

这是我的 Spring Controller ,它有什么问题吗?它不打印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;
}

最佳答案

您不能使用两个@RequestBody,因为它只能绑定(bind)到单个对象(主体只能使用一次)

您需要使用@RequestParam String CropId而不是RequestBody。

参见here澄清

更新:这是您的 Controller 方法,如下所示

@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; 
        } 
    } 
}

关于java - 为什么 Controller 内的字符串不打印任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53390912/

相关文章:

android - 安卓相机教程(使用surfaceview)

java - 无法使用@EnableWebMvc 配置运行 SPRING BOOT 应用程序

java - 批处理文件不会运行 Java Main 类

java - JVM启动后Java类路径是最终的吗?

java - 在使用自定义 genericElement 的情况下如何修复 "No generic element handlers found for namepsace"错误?

java - 如何在GET请求中传递postman中的列表并进入GetMapping

spring-boot - 如何实现动态@ConfigurationProperties 前缀

java - 调用 Minecraft 食谱类(class)

java - 如何使用 okhttp 执行 Reddit 帖子

android - 如何在弹出键盘时保持全屏