【问题标题】:Required MultipartFile parameter 'file' is not present in spring mvcspring mvc中不存在必需的MultipartFile参数'file'
【发布时间】:2015-01-21 23:50:56
【问题描述】:

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

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

选择图片后,我点击上传并看到错误消息:

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

我做错了什么?

【问题讨论】:

  • 嗨,你能把完整的代码发给我吗?我也面临同样的问题。naresh.vadlakonda@darkhorseboa.com 这是我的邮件 ID

标签: java image spring spring-mvc file-upload


【解决方案1】:

你还没有指定name属性,@RequestParam("textFile")需要名字,

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

【讨论】:

    【解决方案2】:

    将名称属性添加到“文件”输入标签

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

    【讨论】:

    • 抱歉,我不应该将名称添加为新的 RequestParam
    【解决方案3】:

    找不到合适解决方案的朋友,别忘了补充

    spring.http.multipart.enabled=true
    

    到你的配置文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2017-01-07
      • 2013-11-06
      • 1970-01-01
      相关资源
      最近更新 更多