1.准备jar包(图标所指必备包,其他按情况导入)
2.项目结构
3.SingleController.java(控制器代码单文件和多文件)
package com.wt.uplaod; import java.io.File;
import java.io.IOException; import javax.servlet.http.HttpSession; 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 SingleController {
//单文件上传
@RequestMapping(value="/upload.html")
public String upload(@RequestParam("uploadFile")MultipartFile file,HttpSession session) throws IllegalStateException, IOException{
//上传文件存储的路径
// String filePath=session.getServletContext().getRealPath("upload");
// System.out.println("存储路径"+filePath);
if(!file.isEmpty()){
//上传文件的全路径
File tempFile=new File("C:/pic"+"/"+file.getOriginalFilename());
System.out.println("全路径:"+tempFile);
//文件上传
file.transferTo(tempFile);
}
return "success";
} //多文件上传
@RequestMapping(value="/upload.html")
public String upload(@RequestParam("uploadFile")MultipartFile[] files,HttpSession session) throws IllegalStateException, IOException{
//上传文件存储的路径
// String filePath=session.getServletContext().getRealPath("upload");
// System.out.println("存储路径"+filePath);
for(MultipartFile file:files){
if(!file.isEmpty()){
//上传文件的全路径
File tempFile=new File("C:/pic"+"/"+file.getOriginalFilename());
System.out.println("全路径:"+tempFile);
//文件上传
file.transferTo(tempFile);
}
}
return "success";
} @RequestMapping("/toFormPage.html")
public String toFormPage(){
return "form";
}
}
4.springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.wt.uplaod"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定编码格式 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传文件的最大值 -->
<property name="maxUploadSize" value="10000000"></property>
<!-- 上传文件临时保存的位置 -->
<property name="uploadTempDir" value="tempDir"></property>
</bean> <!-- 视图解析器。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVC</display-name>
<servlet>
<servlet-name>article6</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>article6</servlet-name>
<url-pattern>/url/*</url-pattern>
</servlet-mapping>
</web-app>
6.form.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="upload.html" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile"><br/>
<input type="file" name="uploadFile"><br/>
<input type="file" name="uploadFile"><br/>
<input type="submit" value="上 传">
</form>
</body>
</html>
7.success.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>
上传成功
</body>
</html>
8.测试访问(单文件上传留一个input就行)
9.注意
如果存储路径为Tomcat的安装路径,那么每次改动form.jsp会自动删除存储的文件夹,可以在控制器中修改存储路径为其他的。