我正在开发一个 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
with Multipart
file.
@RequestParam注解也可以用来关联请求的一部分
“multipart/form-data”请求,带有支持的方法参数
相同的方法参数类型。主要区别在于,当
方法参数不是 String,@RequestParam 依赖于类型
通过注册的 Converter 或 PropertyEditor 进行转换
@RequestPart 依赖于 HttpMessageConverters 考虑
请求部分的“Content-Type”标头。 @RequestParam 很可能
与名称-值表单字段一起使用,而 @RequestPart 可能
与包含更复杂内容的部分(例如 JSON、XML)一起使用。
See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestPart.html http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestPart.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)