本文介绍了Spring MultiPart MediaType不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实际上正在尝试在Spring服务器上上传文件.事实是,我总是遇到415(不受支持的媒体类型)错误,而服务器的日志中却没有任何错误.
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.
有我的代码:
客户端:
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);
}
服务器端:
@POST
@Path("/import/{id}")
@Override
public void importJournalTypeConcurrent(@PathParam("id") long id,
@RequestParam("file") MultipartFile file) {
System.out.println(file.getName());
}
为解决此问题,我还添加了MultipartResolver
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.
推荐答案
来自 MultipartResolver :
声明CommonsMultipartResolver
如下:
@Bean(name = "multipartResolver")
这篇关于Spring MultiPart MediaType不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!