获取HTTP状态400 - 所需的MultipartFile参数“文件”在春天不存在 [英] Geting HTTP Status 400 - Required MultipartFile parameter 'file' is not present in spring
问题描述
我试图用 spring
来上传文件。下面是我的代码我是如何工作
,但如果我尝试使用它,我得到这个响应
:
HTTP状态400 - 所需的MultipartFile参数'file'不存在
我没有得到错误信息。
我使用高级休息客户端进行测试,并将文件作为附件上传。
我的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();
返回您成功上传+ name +!;
} catch(Exception e){
return您上传失败+ name +=>+ e.getMessage();
}
} else {
return由于文件是空的,因此您无法上传+ name +。
$ / code>
需要
< bean id =multipartResolverclass =org.springframework.web.multipart.commons.CommonsMultipartResolver/>
bean来处理文件上传。
$ b 您应该在
应用程序上下文
文件中注册这个bean。 内容类型也应该是有效的。在你的情况 enctype =multipart / form-data
EDIT1:
您可以将上传和内存大小赋予bean属性:
< bean id =multipartResolver
class =org.springframework.web.multipart.commons.CommonsMultipartResolver>
<! - 最大上传大小(以字节为单位) - >
< property name =maxUploadSizevalue =20971520/> <! - 20MB - >
<! - 内存中文件的最大大小(以字节为单位) - >
< property name =maxInMemorySizevalue =1048576/> <! - 1MB - >
< / bean>
I am trying to upload a file using spring
. Below is my code how I am working on it
but if I try to use it I am getting this response
:
HTTP Status 400 - Required MultipartFile parameter 'file' is not present
I dont get what the error is.
I am using advanced rest client for testing and I am uploading file as an attachment.
My 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.";
}
}
Spring needs the
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
bean to handle file-uploads.
You should register this bean in your application context
file.
The Content-Type should also be valid. In your case enctype="multipart/form-data"
EDIT1:
You can give the upload and memory size to the bean properties:
<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>
这篇关于获取HTTP状态400 - 所需的MultipartFile参数“文件”在春天不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!