对多部分文件使用@RequestParam 是正确的方法吗?
Using @RequestParam for multipartfile is a right way?
我正在开发一个 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).
两种注释都可以使用,但是,您可以根据它们在内部解释参数的方式来选择它们。
Spring Docs把它们的区别说得很清楚了:
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. @RequestPara
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).
我正在开发一个 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).
两种注释都可以使用,但是,您可以根据它们在内部解释参数的方式来选择它们。
Spring Docs把它们的区别说得很清楚了:
The main difference is that when the method argument is not a String,
@RequestParam
relies on type conversion via a registered Converter orPropertyEditor
while@RequestPart
relies onHttpMessageConverters
taking into consideration the 'Content-Type' header of the request part.@RequestPara
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).