Spring MultiPart MediaType Unsupported

此生再无相见时 提交于 2019-12-25 08:48:15

问题


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.


回答1:


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")


来源:https://stackoverflow.com/questions/29743649/spring-multipart-mediatype-unsupported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!