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

Java 为什么控制器内的字符串不打印任何内容?,java,android,spring-boot,retrofit2,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());

我正在尝试使用改型发送图像和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");
                }
            });
        }
    }
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);
    }

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

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

有关澄清,请参阅

更新:以下是您的控制器方法

@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; 
        } 
    } 
}
@RequestMapping(value=“/uploadFile”,method=RequestMethod.POST)

public@ResponseBody ResponseEntity不能使用两个@RequestBody,因为它只能绑定到单个对象(body只能使用一次)

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

有关澄清,请参阅

更新:以下是您的控制器方法

@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; 
        } 
    } 
}
@RequestMapping(value=“/uploadFile”,method=RequestMethod.POST)


public@ResponseBody responseentTity您的意思是您的输出只是“字符串是->”?或者它没有打印任何东西?@Stulttuske,没有打印任何东西。它只是打印一些数字。检查代码是否被调用。也许你看错控制台了。System.out.println将在其运行的系统的控制台上打印。您是否在服务器上运行此代码?它仅在有一个参数(即多部分文件)时调用。但当我添加字符串参数时,它不会打印任何内容。控制台上打印的响应如下108 10@Stultuske,我已经与Postman进行了检查,它在只有多部分文件参数时工作。我在传递字符串难度时出错了吗?你的意思是你的输出只是“字符串是->”?或者它没有打印任何东西?@Stulttuske,没有打印任何东西。它只是打印一些数字。检查代码是否被调用。也许你看错控制台了。System.out.println将在其运行的系统的控制台上打印。您是否在服务器上运行此代码?它仅在有一个参数(即多部分文件)时调用。但当我添加字符串参数时,它不会打印任何内容。控制台上打印的响应如下108 10@Stultuske,我已经与Postman进行了检查,它在只有多部分文件参数时工作。我在传递字符串艰巨性时出错了吗?如何使用具有多个文件请求和字符串的POJO类?我可以将其传递到RequestBody?您可以做的是让映像进入body,并让参数进入request参数。我需要SSL加密,body提供加密,而params不提供加密。这是由spring提供的。我可以在参数内发送图像,在主体内发送Id吗?有没有任何方法可以通过POJO发送文件?我试过了,但不起作用。我尝试过这样的方法->公共类CropImageRequest{private String cropId;private MultipartBody.Part cropImage;public String getCropId(){return cropId;}public void setCropId(String cropId){this.cropId=cropId;}公共多部分体.Part getCropImage(){return cropImage;}public void setCropImage(MultipartBody.Part cropImage){this.cropImage=cropImage;}如何使用具有多个文件请求和字符串的POJO类?我可以将其传递到RequestBody中?您可以做的是让映像进入正文和请求参数中的参数。我需要SSL加密,正文提供加密,而params不提供加密。这是由spring提供的。我可以在正文中发送image inside参数和Id吗?是否有我可以通过POJO发送文件吗?我已经尝试过了,但它不起作用。我已经尝试过这样的方法->公共类CropImageRequest{private String cropId;private MultipartBody.Part cropImage;公共字符串getCropId(){return cropId;}public void setCropId(String cropId){this.cropId=cropId;}public MultipartBody.Part getCropImage(){return cropImage;}public void setCropImage(MultipartBody.Part cropImage){this.cropImage=cropImage;}
@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; 
        } 
    } 
}