Geting HTTP Status 400 - Required MultipartFile "file" parameter missing in spring

I am trying to upload a file using spring . Below is my code, how I work on it, but if I try to use it, I get this response :

HTTP Status 400 - Required MultipartFile File parameter

I do not understand what a mistake is.

I use the pre-client for testing, and I upload the 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."; } } 
+7
source share
4 answers

Spring is required

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

bean for handling file downloads.

You must register this bean in your application context file.

The content type must also be valid. In your case, enctype="multipart/form-data"

EDIT1:

You can specify the size of the upload and memory in 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> 
+7
source

when you select the pre-vacation client file, on the right side is the input field enter the parameter name in the input field , in your case, the parameter name is file

The parameter name specified here in the @RequestParam controller ("file")

enter image description here

+2
source

it works for me after I wrote the name of the parameter β€œfile” in this input field, when I already configured the component identifier well in the example of the input Krajee boot file (" https://github.com/kartik-v/bootstrap- fileinput ").

0
source

For those who can’t find a suitable solution, do not forget to add a thymeleaf dependency to your project

For instance;

 compile("org.springframework.boot:spring-boot-starter-thymeleaf") 
0
source

All Articles