4

测试一个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参数'文件'不存在

4

1 回答 1

10

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

改变:

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

至:

new MockMultipartFile("file", "myFile.txt", "text/plain", "hello".getBytes());
于 2013-09-23T12:09:10.357 回答