java Spring mvc:HTTP 状态 400 - 所需的 MultipartFile 参数“文件”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25830503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:45:23  来源:igfitidea点击:

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

javaspring-mvcfile-upload

提问by Stepan Yakovenko

This is my controller:

这是我的控制器:

@RequestMapping(value = "/admin/users/photo", method = RequestMethod.POST)
public ModelAndView editUser(@RequestParam("file") MultipartFile file,
                             Principal principal) {
    // code never gets here
}

my mvc-dispatcher-servlet.xml

我的 mvc-dispatcher-servlet.xml

<bean id="multipartResolver"
      class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>

pom.xml:

pom.xml:

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

form itself:

形式本身:

<form action="${pageContext.request.contextPath}"
      method="POST"
      enctype="multipart/form-data">
        <input type="file"
        name="file"
        />
    <input type="submit" value="Добавить">
</form>

This is what browser says:

浏览器是这样说的:

http 400

http 400

How to fix that?

如何解决?

回答by stacky

You can try to use this

你可以尝试使用这个

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

instead of

代替

<bean id="multipartResolver"
      class="org.springframework.web.multipart.support.StandardServletMultipartResolver">