java HTTP 状态 400 - 必需 ... 参数 ... 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27471781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:51:28 来源:igfitidea点击:
HTTP Status 400 - Required ... parameter ... is not present
提问by gstackoverflow
I have following controller:
我有以下控制器:
@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;
}
}
and following jsp:
和以下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>
When I submit form I get following error:
当我提交表单时,我收到以下错误:
HTTP Status 400 - Required MultipartFileWrapper parameter 'file' is not present
What do I make wrong?
我做错了什么?
回答by gstackoverflow
I removed @RequestParam
annotation and it works now:
我删除了@RequestParam
注释,现在可以使用了:
replace
代替
public ResponseEntity<String> handleFileUpload(@RequestParam("file") @Validated MultipartFileWrapper file, BindingResult result, Principal principal)
with
和
public ResponseEntity<String> handleFileUpload( @Validated MultipartFileWrapper file, BindingResult result, Principal principal)