![](/img/trans.png)
[英]Spring mvc: HTTP Status 400 - Required MultipartFile parameter 'file' is not present
[英]Geting HTTP Status 400 - Required MultipartFile parameter 'file' is not present in spring
我正在尝试使用spring
上传文件。 下面是我的代码我是如何处理它但如果我尝试使用它我得到这个response
:
HTTP状态400 - 不存在所需的MultipartFile参数“file”
我不知道错误是什么。
我正在使用高级休息客户端进行测试,我将文件作为附件上传。
我的Javacode:
@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" />
bean来处理文件上传。
您应该在application context
文件中注册此bean。
Content-Type也应该有效。 在你的情况下enctype="multipart/form-data"
EDIT1:
您可以将上传和内存大小赋予bean属性:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean>
当我在Krajee bootstrap inputfile示例中已经很好地配置了bean id之后,我在参数“file”的输入框名称中写入后,它对我有用(“ https://github.com/kartik-v/bootstrap-fileinput “)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.