Error message = Required MultipartFile parameter 'file' is not present

被刻印的时光 ゝ 提交于 2019-12-23 15:13:25

问题


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) {}

and the test this

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

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

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


回答1:


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

Change:

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

To:

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


来源:https://stackoverflow.com/questions/18833683/error-message-required-multipartfile-parameter-file-is-not-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!