Java spring mvc 中不存在必需的 MultipartFile 参数“文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27101931/
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
Required MultipartFile parameter 'file' is not present in spring mvc
提问by gstackoverflow
I am trying to add feature of uploading picture to my spring mvc application.
我正在尝试添加将图片上传到我的 spring mvc 应用程序的功能。
jsp part:
jsp部分:
...
<form method="POST" action="uploadImage" enctype="multipart/form-data">
<div class="load-line">
<input type="file" class="file"/>
<input type="submit" value="Upload">
...
configuration:
配置:
...
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
...
controller:
控制器:
@RequestMapping(value="/member/createCompany/uploadImage", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(
@RequestParam("file") MultipartFile file){
String name = "image_name";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
After I selected picture I click upload and see error message:
选择图片后,我单击上传并查看错误消息:
HTTP Status 400 - Required MultipartFile parameter 'file' is not present
What do I wrong?
我怎么了?
采纳答案by Santhosh
You have not specified the name
attribute , @RequestParam("textFile")
requires name ,
您尚未指定name
属性,@RequestParam("textFile")
需要名称,
<input type="file" class="file" name="textFile"/>
回答by Dmitry
add name attribute to "file" input tag
将名称属性添加到“文件”输入标签
<input type="file" class="file" name="file"/>
回答by Ad Infinitum
For those, who cannot find a suitable solution, please do not forget to add
对于那些找不到合适解决方案的人,请不要忘记添加
spring.http.multipart.enabled=true
to your configuration file
到您的配置文件