Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required MultipartFile parameter 'file' is not present in spring mvc

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?

like image 899
gstackoverflow Avatar asked Nov 24 '14 09:11

gstackoverflow


People also ask

What is Multipartfile spring?

A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired.

What do you understand by MultipartResolver?

MultipartResolver interface is used for uploading files – CommonsMultipartResolver and StandardServletMultipartResolver are two implementations provided by spring framework for file uploading.


1 Answers

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

 <input type="file" class="file" name="textFile"/>
like image 76
Santhosh Avatar answered Oct 13 '22 12:10

Santhosh