Spring Boot multipartfile always null

前端 未结 5 1773
悲&欢浪女
悲&欢浪女 2021-02-04 10:02

I am using Spring Boot version = \'1.4.0.RC1\' with Spring Boot Stormpath 1.0.2.

I am trying to use multipart file upload but the MultipartFile is always null in the con

相关标签:
5条回答
  • 2021-02-04 10:21

    Please refer to my answer in this URL: Spring Boot multipart upload getting null file object

    Basically you have to avoid the default Spring configuration for multipart and avoid to set Content-type or boundary in your request header.

    0 讨论(0)
  • 2021-02-04 10:27

    You have to enable the Spring Multipart Resolver as by default Spring doesn't enable the multipart capability.

    By default, Spring does no multipart handling, because some developers want to handle multiparts themselves. You enable Spring multipart handling by adding a multipart resolver to the web application’s context.

    To your configuration class you would want to add the following bean:

    @Bean
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
    

    * Update * As my previous answer was not correct based on the comments. Here is an updated example that I was able to run successfully.

    @SpringBootApplication
    public class StackoverflowWebmvcSandboxApplication {
        public static void main(String[] args) {
            SpringApplication.run(StackoverflowWebmvcSandboxApplication.class, args);
        }
    
        @Controller
        public class UploadPhoto {
            @PostMapping("{username}/profilePhoto")
            public ResponseEntity<String> saveProfilePhoto(@PathVariable("username") String username,
                    @RequestPart(name = "file", required = false) MultipartFile imageFile, HttpServletRequest request) {
                String body = "MultipartFile";
                if (imageFile == null) {
                    body = "Null MultipartFile";
                }
    
                return ResponseEntity.status(HttpStatus.CREATED).body(body);
            }
        }
    }
    

    It is a very basic test with no special stuff. I then created a postman request and here is the sample curl call:

    curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: 17e5e6ac-3762-7d45-bc99-8cfcb6dc8cb5" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "file=@" "http://localhost:8080/test/profilePhoto"
    

    The response was MultipartFile meaning that it wasn't null and doing a debug on that line showed that the variable was populated with the image that I was uploading.

    0 讨论(0)
  • 2021-02-04 10:31

    Check if the following line is present in your application.properties:

    spring.http.multipart.enabled = true
    
    0 讨论(0)
  • 2021-02-04 10:32

    I found out that the problem was the way I was building my request with Retrofit.

    Spring's multipart resolver requires the filename for the file to be present in content-disposition field of the part. Without this, it doesn't add the file into the multipart request.

    According to the info found here: https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server, my API interface should be:

    @Multipart
    @POST("users/{username}/profilePhoto")
    Call<Void> uploadProfilePhoto(@Path("username") String username,
                                  @Part MultipartBody.Part profilePhoto);
    

    And then when making the call in my test:

    // Given
    String usernamme = usernames[0];
    Resource testImageResource = context.getResource("classpath:images/test_image.jpg");
    File imageFile = testImageResource.getFile();
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
    MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", imageFile.getName(), requestFile);
    
    // When
    Response<Void> response = testApi.uploadProfilePhoto(usernamme, filePart).execute();
    
    0 讨论(0)
  • 2021-02-04 10:34

    You have to enable the Spring Multipart Resolver as by default Spring doesn't enable the multipart capability.

    By default, Spring does no multipart handling, because some developers want to handle multiparts themselves. You enable Spring multipart handling by adding a multipart resolver to the web application’s context.

    To your configuration class you would want to add the following bean:

    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }
    

    Since spring boot 1.4+ uses servlet 3.0+ we can leverage on StandardServletMultipartResolver instead of classic CommonsMultipartResolver

    0 讨论(0)
提交回复
热议问题