1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2.配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 自动扫面cn文件夹下的所有注解 -->
<context:component-scan base-package="cn"/>
<!-- 使注解有效 -->
<tx:annotation-driven/>
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 多部分解析器,用来支持文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定默认的编码格式,不指定的时候 使用iso-8859-1 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 上传文件大小的最大值,单位是字节 -->
<property name="maxUploadSize" value="10000000"/>
<!-- 文件上传的临时目录 -->
<property name="uploadTempDir" value="tempDir"/>
</bean>
</beans>
3.创建single.jsp(单文件)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>单文件上传</title>
</head>
<body>
<form action="doUpload.do" method="post" enctype="multipart/form-data">
<input type="file" name="singleFile">
<input type="submit" value="上传">
</form>
</body>
</html>
4.创建multi.jsp(多文件)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doMulti.do" method="post" enctype="multipart/form-data">
<input type="file" name="files">
<br/>
<input type="file" name="files">
<br/>
<input type="file" name="files">
<br/><br/>
<input type="submit" value="多文件上传">
</form>
</body>
</html>
5.定义controller类testController(单文件)
package cn.controller;
import java.io.File;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class testController {
@RequestMapping("/test.do")
public String text(){
return "single";
}
@RequestMapping("/doUpload.do")
public String doUpload(@RequestParam("singleFile")MultipartFile file) throws IllegalStateException, IOException{
if(!file.isEmpty()){
String targPath = "C:/Users/陈磊/Desktop/path";
File tempFile = new File(targPath+"/"+file.getOriginalFilename());
file.transferTo(tempFile);
return "text";
}else{
return "single";
}
}
}
6.定义controller类MultiController(多文件)
package cn.controller;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class MultiController {
@RequestMapping("/toMultiPage.do")
public String toMultiPage(){
return "multi";
}
@RequestMapping("/doMulti.do")
public String doMulti(@RequestParam MultipartFile[] files,HttpServletRequest request) throws IllegalStateException, IOException{
//判断是否接收到了多个文件
if(files.length>0){
System.out.println(files.length);
//获取的是服务器下的文件路径
String realPath = request.getSession().getServletContext().getRealPath("upload");
for (MultipartFile file : files) {
//获取原始文件名
String orgName = file.getOriginalFilename();
//获取后缀
String suffix = orgName.substring(orgName.indexOf(".")-1,orgName.length());
//通过uuid工具获取不会重复的文件名
String fileName=UUID.randomUUID()+suffix;
//逐个处理文件
File tempFile=new File(realPath+"/"+fileName);
//文件转换
file.transferTo(tempFile);
}
}
return "text";
}
}