我正在尝试使用 spring
上传文件。以下是我的代码:
但是,如果我尝试使用它,我会得到这个 response
:
HTTP状态400 - 缺少必需的MultipartFile参数“file”
我不知道错误在哪里。
我正在使用高级REST客户端进行测试,并将文件作为附件上传。
我的Java代码:
@RequestMapping(value = "/upload",headers = "Content-Type=multipart/form-data", method = RequestMethod.POST)
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file)
{
String name= "test.xlsx";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + "!";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
这段代码? - Patrick