java – 错误消息=必需的MultipartFile参数’file’不存在

测试一个spring文件上传表单,controlelr签名看起来像这样

@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’不存在

最佳答案
您将参数命名为“file”而不是“content”:

更改:

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

至:

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

转载注明原文:java – 错误消息=必需的MultipartFile参数’file’不存在 - 代码日志