出错的位置在 spring-mvc.xml 中,将文件上传解析器的 id 写错了!!!
具体代码如下:
spring-mvc.xml
<!--配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--上传文件总大小-->
<property name="maxUploadSize" value="5242800"/>
<!--上传单个文件的大小-->
<property name="maxUploadSizePerFile" value="5242800"/>
<!--上传文件的编码类型-->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
jsp页面
<form action="${pageContext.request.contextPath}/user/quick22" method="post" enctype="multipart/form-data">
名称:<input type="text" name="username"><br>
文件:<input type="file" name="uploadFile"><br>
<input type="submit" value="提交"><br>
</form>
pom.xml
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Controller
@RequestMapping(value = "/quick22")
@ResponseBody //告知SpringMVC框架,不进行视图跳转,直接进行数据响应
public void save22(String username, MultipartFile uploadFile) throws IOException {
System.out.println(username);
System.out.println(uploadFile);
}