2013-09-16 52 views
4

測試彈簧文件上傳表單中,controlelr簽名看起來像這樣錯誤消息=必填MultipartFile參數「文件」不存在

@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參數'文件'不存在

+2

調用測試構造函數時,您將它命名爲 「內容」。 – Bart

+0

@謝謝你,這是我一生中的十五分鐘,我永遠不會回來 – NimChimpsky

回答

7

您將參數命名爲「file」,並將n OT 「內容」:

變化:

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

要:

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

Ahhhhhhhhhh .....很好。我也是這樣做的。 – Kieveli

+0

工作給我。謝謝 – ryzhman

相關問題