我正在开发一个spring mvc应用程序,我想在我的控制器中处理多部分请求.在请求中我也传递了MultiPartFile,目前我正在使用@RequestParam获取文件参数,方法看起来像,
@RequestMapping(method = RequestMethod.POST)
public def save(
@ModelAttribute @Valid Product product,
@RequestParam(value = "image", required = false) MultipartFile file) {
.....
}
上面的代码在我的服务中运行良好,文件正在服务器端.现在在某个地方我看到,如果文件需要使用@RequestPart注释而不是@RequestParam.使用@RequestParam文件有什么不对吗?或者它将来可能会导致任何错误?
解决方法:
将@RequestParam与Multipart文件一起使用并没有错.
@RequestParam annotation can also be used to associate the part of a
“multipart/form-data” request with a method argument supporting the
same method argument types. The main difference is that when the
method argument is not a String, @RequestParam relies on type
conversion via a registered Converter or PropertyEditor while
@RequestPart relies on HttpMessageConverters taking into consideration
the ‘Content-Type’ header of the request part. @RequestParam is likely
to be used with name-value form fields while @RequestPart is likely to
be used with parts containing more complex content (e.g. JSON, XML).