我正在尝试将上传图片的功能添加到我的spring mvc应用程序中.
jsp部分:
...
...
组态:
...
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
...
控制器:
@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.";
}
}
选择图片后,我点击上传并看到错误信息:
HTTP Status 400 - Required MultipartFile parameter 'file' is not present
我错了什么?