具体代码请访问springmvc的具体操作步骤

1.导入坐标

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

2.编写upload.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/report22" method="post" enctype="multipart/form-data">
名称:<input type="text" name="username"><br>
文件:<input type="file" name="upload"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

3.配置spring-mvc配置文件

<!--    配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="50000"/>

</bean>

4.编写Controller层

@RequestMapping("/report22")
@ResponseBody
public void save22(String username, MultipartFile upload) throws IOException {
System.out.println(username);
//获得上传文件名称
String originalFilename = upload.getOriginalFilename();
//上传到哪个磁盘路径下
upload.transferTo(new File("G:\\upload\\"+originalFilename));
}

5.启动tomcat访问http://localhost:8080/upload.jsp

然后填写张三,选择emp.txt文件点击提交

6.结果

SpringMVC-文件上传_上传

SpringMVC-文件上传_上传_02

//多文件上传可以采用数组形式