gpt4 book ai didi

java - 将@RequestParam 用于多部分文件是正确的方法吗?

转载 作者:IT老高 更新时间:2023-10-28 13:57:22 24 4
gpt4 key购买 nike

我正在开发一个 spring mvc 应用程序,我想在我的 Controller 中处理多部分请求。在请求中我也传递了 MultiPartFile,目前我正在使用 @RequestParam 来获取文件参数,方法看起来像,

@RequestMapping(method = RequestMethod.POST)
public def save(
@ModelAttribute @Valid Product product,
@RequestParam(value = "image", required = false) MultipartFile file) {
.....
}

以上代码在我的服务中运行良好,并且文件正在服务器端。现在我在某个地方看到,如果该文件需要使用 @RequestPart 注释而不是 @RequestParam。将 @RequestParam 用于文件有什么问题吗?或者将来可能会导致任何类型的错误?

最佳答案

使用 @RequestParamMultipart 文件没有问题。

@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

关于java - 将@RequestParam 用于多部分文件是正确的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38156646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com