简体   繁体   中英

Spring MultiPart MediaType Unsupported

I'm actualy trying to upload a file on my Spring server. The fact is that I always have a 415 (Unsupported Media Type) error without any error in server's log.

There is my code :

Client side :

  journal.import= function(id, file, callbackSuccess, callbackError){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(config.API_URL +"/newspapper/import/"+id, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
        }).success(callbackSuccess).error(callbackError);
    }

Server side :

@POST
@Path("/import/{id}")
@Override
public void importJournalTypeConcurrent(@PathParam("id") long id,
        @RequestParam("file") MultipartFile file) {
    System.out.println(file.getName());
}

To solve this problem, I've also add a MultipartResolver

@Bean
public CommonsMultipartResolver getMultipartResolver() {
    return new CommonsMultipartResolver();
}

It's probably something stupid, but I can't find what I missed.

From documentation of MultipartResolver :

To define an implementation, create a bean with the id "multipartResolver" in a DispatcherServlet's application context

Declare the CommonsMultipartResolver as below:

@Bean(name = "multipartResolver")

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