将@RequestParam用于multipartfile是正确的方法吗? [英] Using @RequestParam for multipartfile is a right way?
问题描述
我正在开发spring mvc应用程序,我想在控制器中处理多部分请求.在请求中,我还传递了MultiPartFile
,当前我正在使用@RequestParam
来获取文件参数,该方法看起来像
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,
@RequestMapping(method = RequestMethod.POST)
public def save(
@ModelAttribute @Valid Product product,
@RequestParam(value = "image", required = false) MultipartFile file) {
.....
}
上面的代码在我的服务中效果很好,并且文件在服务器端.现在在某个地方,我看到了在文件情况下需要使用@RequestPart
注释而不是@RequestParam
的情况.将@RequestParam
用于文件有什么错误吗?还是将来可能导致任何类型的错误?
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?
推荐答案
在Multipart
文件中使用@RequestParam
没错.
@RequestParam批注还可用于关联广告的一部分 支持方法参数的"multipart/form-data"请求 相同的方法参数类型.主要区别在于 方法参数不是字符串,@ RequestParam依赖于类型 通过注册的Converter或PropertyEditor进行转换 @RequestPart依赖于HttpMessageConverters并将其考虑在内 请求部分的"Content-Type"标头. @RequestParam可能 与名称-值表单字段一起使用,而@RequestPart可能会 与包含更复杂内容(例如JSON,XML)的部分一起使用.
@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用于multipartfile是正确的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!