Required MultipartFile parameter 'file' is

2020-02-05 12:01发布

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?

3条回答
淡お忘
2楼-- · 2020-02-05 12:28

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

spring.http.multipart.enabled=true

to your configuration file

查看更多
家丑人穷心不美
3楼-- · 2020-02-05 12:38

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

 <input type="file" class="file" name="textFile"/>
查看更多
地球回转人心会变
4楼-- · 2020-02-05 12:48

add name attribute to "file" input tag

<input type="file" class="file" name="file"/>
查看更多
登录 后发表回答