Required MultipartFile parameter 'file' is not present in spring mvc
Asked Answered
D

4

17

I am trying to add feature of uploading picture to my spring mvc application.

jsp part:

...
<form method="POST"  action="uploadImage" enctype="multipart/form-data">
                <div class="load-line">
                    <input type="file" class="file"/>
                    <input type="submit" value="Upload">
...

configuration:

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

controller:

 @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.";
        }
    }

After I selected picture I click upload and see error message:

HTTP Status 400 - Required MultipartFile parameter 'file' is not present

What do I wrong?

Durwyn answered 24/11, 2014 at 9:52 Comment(1)
hi can you send me the full code please am facing same [email protected] this is my mail idPadishah
M
19

You have not specified the name attribute , @RequestParam("textFile") requires name ,

 <input type="file" class="file" name="textFile"/>
Mexican answered 24/11, 2014 at 9:55 Comment(0)
P
11

add name attribute to "file" input tag

<input type="file" class="file" name="file"/>
Parsons answered 24/11, 2014 at 9:57 Comment(1)
Sorry, I shouldn't add name as new RequestParamDurwyn
K
2

For those, who cannot find a suitable solution, please do not forget to add

spring.http.multipart.enabled=true

to your configuration file

Karakul answered 1/12, 2018 at 16:2 Comment(0)
I
1

For Springboot: If your application does not use spring-boot-starter-web and spring-boot-starter-data-rest, instead it uses "spring-boot-starter-webflux", then you will get the error "HTTP Status 400 - Required MultipartFile parameter 'file' is not present" when you use "@RequestParam("file") MultipartFile file" in your POST request.

Since webflux is a reactive library, MultipartFile is not supported.

You need to follow this link for reactive-programming way to upload form-data. https://github.com/entzik/reactive-spring-boot-examples/blob/master/src/main/java/com/thekirschners/springbootsamples/reactiveupload/ReactiveUploadResource.java

I have tried a lot. For webflux only application, the above solution alone works.

Interdictory answered 9/9, 2022 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.