Using @RequestParam for multipartfile is a right way?

前端 未结 2 1946
北恋
北恋 2020-12-06 10:08

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\'

相关标签:
2条回答
  • 2020-12-06 10:41

    It is nothing wrong using @RequestParam with Multipart file.

    @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).

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

    0 讨论(0)
  • 2020-12-06 10:58

    Both of the Annotations can be used, however, you can make your choice about them based on how they interpret arguments internally.

    The Spring Docs states 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 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).

    0 讨论(0)
提交回复
热议问题