将@RequestParam用于multipartfile是正确的方法吗? [英] Using @RequestParam for multipartfile is a right way?

查看:272
本文介绍了将@RequestParam用于multipartfile是正确的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发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).

请参见 http ://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestPart.html

这篇关于将@RequestParam用于multipartfile是正确的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆