spring mvc 中不存在必需的 MultipartFile 参数“文件" [英] Required MultipartFile parameter 'file' is not present in spring mvc

查看:41
本文介绍了spring mvc 中不存在必需的 MultipartFile 参数“文件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加将图片上传到我的 spring mvc 应用程序的功能.

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

jsp部分:

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

配置:

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

控制器:

 @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

我做错了什么?

推荐答案

您没有指定 name 属性,@RequestParam("textFile") 需要名称,

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

 <input type="file" class="file" name="textFile"/>

这篇关于spring mvc 中不存在必需的 MultipartFile 参数“文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆