错误消息=必需的MultipartFile参数'file'不存在 [英] Error message = Required MultipartFile parameter 'file' is not present

查看:250
本文介绍了错误消息=必需的MultipartFile参数'file'不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试spring文件上传表单,controlelr签名如下所示

Testing a spring file upload form, the controlelr signature looks like this

@RequestMapping(value = "upload", method = RequestMethod.POST)
@ResponseBody
public void upload(@RequestParam("file") MultipartFile multipartFile) {}

并对此进行测试

final MockMultipartFile file 
  = new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes());

MockHttpServletRequestBuilder mockHttpServletRequestBuilder = 
  .fileUpload("/upload/")
  .file(file)
  .accept(MediaType.APPLICATION_JSON);

但我得到了上述提示:错误消息=所需的MultipartFile参数'file'不存在

but I get the aforementioned : Error message = Required MultipartFile parameter 'file' is not present

推荐答案

您将参数命名为文件"而不是内容":

You named the parameter as "file" and not as "content":

更改:

new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes());

收件人:

new MockMultipartFile("file", "myFile.txt", "text/plain", "hello".getBytes());

这篇关于错误消息=必需的MultipartFile参数'file'不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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