java 对 multipartfile 使用 @RequestParam 是正确的方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38156646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using @RequestParam for multipartfile is a right way?
提问by Pranav C Balan
I'm developing a spring mvc application and I want to handle multipart request in my controller. In the request I'm passing MultiPartFile
also, currently I'm using @RequestParam
to get the file paramaeter, the method look like,
我正在开发一个 spring mvc 应用程序,我想在我的控制器中处理多部分请求。在我MultiPartFile
也传递的请求中,目前我正在使用@RequestParam
获取文件参数,该方法如下所示,
@RequestMapping(method = RequestMethod.POST)
public def save(
@ModelAttribute @Valid Product product,
@RequestParam(value = "image", required = false) MultipartFile file) {
.....
}
Above code works well in my service and the file is getting on the server side. Now somewhere I seen that in case of file need to use @RequestPart
annotation instead of @RequestParam
. Is there anything wrong to use @RequestParam
for file ? Or it may cause any kind of error in future?
上面的代码在我的服务中运行良好,文件正在服务器端。现在我在某处看到文件需要使用@RequestPart
注释而不是@RequestParam
. @RequestParam
用于文件有什么问题吗?或者将来可能会导致任何类型的错误?
采纳答案by Wilson
It is nothing wrong using @RequestParam
with Multipart
file.
它是用什么错@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).
@RequestParam 注释还可用于将“multipart/form-data”请求的一部分与支持相同方法参数类型的方法参数相关联。主要区别在于,当方法参数不是字符串时,@RequestParam 依赖于通过注册的 Converter 或 PropertyEditor 进行的类型转换,而@RequestPart 依赖于 HttpMessageConverters,并考虑到请求部分的“Content-Type”标头。@RequestParam 可能用于名称-值表单字段,而@RequestPart 可能用于包含更复杂内容的部分(例如 JSON、XML)。
回答by Chaturvedi Saurabh
Both of the Annotations can be used, however, you can make your choice about them based on how they interpret arguments internally.
这两个注释都可以使用,但是,您可以根据它们在内部解释参数的方式来选择它们。
The Spring Docsstates the difference between them very clearly:
在春天文档指出它们之间的区别很清楚:
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).
主要的区别是,当该方法参数不是字符串,
@RequestParam
依赖于经由注册转换的类型转换或PropertyEditor
同时@RequestPart
依赖于HttpMessageConverters
考虑到请求部分的“内容类型”标头。@RequestPara
可能与名称-值表单字段@RequestPart
一起使用,而可能与包含更复杂内容的部分(例如 JSON、XML)一起使用。