我有以下控制器:
@ModelAttribute("multipartFileWrapper")
public MultipartFileWrapper createEmployeeModel() {
return new MultipartFileWrapper();
}
@RequestMapping(value = "/member/createCompany/uploadImage", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> handleFileUpload(@RequestParam("file") @Validated MultipartFileWrapper file, BindingResult result, Principal principal) {
if(result.hasErrors()){
System.out.println("ololololo");
}
return null;
}
}
@Component
public class MultipartFileWrapper {
@Extensions(".jpg")
MultipartFile multipartFile;
public MultipartFile getMultipartFile() {
return multipartFile;
}
public void setMultipartFile(MultipartFile multipartFile) {
this.multipartFile = multipartFile;
}
}
并遵循jsp:
<form:form method="POST" action="uploadImage" enctype="multipart/form-data" id="imageUploadForm" commandName="multipartFileWrapper" >
<div class="load-line">
<td><form:input path="multipartFile" name="file" type = "file" class = "file" accept=".jpg,.png,.gif,.bmp,.wbmp" /></td>
<td><form:errors path="multipartFile" cssClass="error" /></td>
<input type="submit" value="Submit" />
</div>
</form:form>
当我提交表单时,我收到以下错误:
HTTP Status 400 - Required MultipartFileWrapper parameter 'file' is not present
我做错了什么?
答案 0 :(得分:3)
我删除了@RequestParam
注释,现在可以使用了:
替换
public ResponseEntity<String> handleFileUpload(@RequestParam("file") @Validated MultipartFileWrapper file, BindingResult result, Principal principal)
带
public ResponseEntity<String> handleFileUpload( @Validated MultipartFileWrapper file, BindingResult result, Principal principal)