————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-
我将用自认为最简单的语言,描述Springmvc的文件上传,来将老夫毕生功力灌顶传授给你
首先文件上传,又简至深
前提有吗?jar包,form表单里的属性(method="post" enctype="multipart/form-data")
jar包的节点我给出来:
1
2
3
4
5
6
7
8
9
10
11
12
13 1 <!--文件上传的jar包-->
2 <dependency>
3 <groupId>commons-fileupload</groupId>
4 <artifactId>commons-fileupload</artifactId>
5 <version>1.3.1</version>
6 </dependency>
7
8 <dependency>
9 <groupId>commons-io</groupId>
10 <artifactId>commons-io</artifactId>
11 <version>1.4</version>
12 </dependency>
13
下面我开始我第一个案例,最简单的文件上传:
1.jsp页面:fileupload.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1<%--
2 Created by IntelliJ IDEA.
3 User: Dawn
4 Date: 2018/4/2
5 Time: 14:19
6 To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
9<html>
10<head>
11 <title>文件上传</title>
12</head>
13<body>
14<h1>文件上传</h1>
15 <form action="${pageContext.request.contextPath}/fileuploadfirst" method="post" enctype="multipart/form-data">
16 文件1 <input type="file" name="upload"/>
17 <input type="submit"/>
18 </form>
19</body>
20</html>
21
success.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1<%--
2 Created by IntelliJ IDEA.
3 User: Dawn
4 Date: 2018/4/2
5 Time: 14:19
6 To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
9<html>
10<head>
11 <title>成功</title>
12</head>
13<body>
14 <h1>SUCCESS</h1>
15</body>
16</html>
17
2.在webapp下你jsp页面的那个包下创建一个文件夹,我的叫upload,里面随便扔个东西,编译后,如果即使这样target目录下还没有upload这个文件夹的话,就手动创建
3.创建处理器和处理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 1package cn.dawn.day24fileupload;
2
3import com.sun.org.glassfish.gmbal.ParameterNames;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RequestParam;
7import org.springframework.web.multipart.MultipartFile;
8
9import javax.servlet.http.HttpSession;
10import java.io.File;
11import java.io.IOException;
12
13/**
14 * Created by Dawn on 2018/4/2.
15 */
16@Controller
17public class FileupLoad {
18
19
20 /*最初始版本*/
21 @RequestMapping("/fileuploadfirst")
22 public String fileuploadfirst(MultipartFile upload, HttpSession session){
23 /*获取上传文件的简单名称例如 1.txt*/
24 String childrlPath = upload.getOriginalFilename();
25 /*获得一个真实路径*/
26 String parentPath = session.getServletContext().getRealPath("/day24/upload");
27 /*获取一个完整的文件对象*/
28 File file=new File(parentPath,childrlPath);
29 /*传输创建到本地*/
30 try {
31 upload.transferTo(file);
32 /*上传成功*/
33 return "success";
34 } catch (IOException e) {
35 e.printStackTrace();
36 }
37
38 /*上传失败*/
39 return "fileupload";
40 }
41}
42
4.自己的xml配置文件:这儿其实我想删减点的,文件名中文的处理和文件大小限制放在后面讲也行,不过放在这儿,你们应该也能理解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/mvc
9 http://www.springframework.org/schema/mvc/spring-mvc.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context.xsd">
12
13 <!--包扫描器-->
14 <context:component-scan base-package="cn.dawn.day24fileupload"></context:component-scan>
15 <!--视图解析器-->
16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17 <property name="prefix" value="/day24/"></property>
18 <property name="suffix" value=".jsp"></property>
19 </bean>
20
21 <!--多部分文件解析器-->
22 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
23 <!--文件名的编码-->
24 <property name="defaultEncoding" value="UTF-8"></property>
25 <!--限制文件大小--><!--这个单位是byte,我这儿限制的是20mb最大容量-->
26 <property name="maxUploadSize" value="20971520"></property>
27 </bean>
28
29 <!--绑定注解驱动-->
30 <mvc:annotation-driven></mvc:annotation-driven>
31
32</beans>
33
5.web.xml中修改中央处理器的上下文配置参数为上面那个xml
6.启动tomcat,访问fileupload.jsp页面
第二个案例:多文件上传
1.jsp页面fileuploadmore.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1<%--
2 Created by IntelliJ IDEA.
3 User: Dawn
4 Date: 2018/4/2
5 Time: 14:19
6 To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
9<html>
10<head>
11 <title>文件上传</title>
12</head>
13<body>
14<h1>文件上传</h1>
15 <form action="${pageContext.request.contextPath}/fileuploadsecond" method="post" enctype="multipart/form-data">
16 文件1 <input type="file" name="upload"/>
17 文件2 <input type="file" name="upload"/>
18 文件3 <input type="file" name="upload"/>
19 <input type="submit"/>
20 </form>
21</body>
22</html>
23
success.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1<%--
2 Created by IntelliJ IDEA.
3 User: Dawn
4 Date: 2018/4/2
5 Time: 14:19
6 To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
9<html>
10<head>
11 <title>成功</title>
12</head>
13<body>
14 <h1>SUCCESS</h1>
15</body>
16</html>
17
2.处理器处理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 1package cn.dawn.day24fileupload;
2
3import com.sun.org.glassfish.gmbal.ParameterNames;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RequestParam;
7import org.springframework.web.multipart.MultipartFile;
8
9import javax.servlet.http.HttpSession;
10import java.io.File;
11import java.io.IOException;
12
13/**
14 * Created by Dawn on 2018/4/2.
15 */
16@Controller
17public class FileupLoad {
18
19 /*多文件版本*/
20 @RequestMapping("/fileuploadsecond")
21 public String fileuploadsecond(@RequestParam MultipartFile[] upload, HttpSession session){
22 for (MultipartFile item :upload) {
23 if(item.getSize()>0) {
24 /*获取上传文件的简单名称例如 1.txt*/
25 String childrlPath = item.getOriginalFilename();
26 /*获得一个真实路径*/
27 String parentPath = session.getServletContext().getRealPath("/day24/upload");
28 /*获取一个完整的文件对象*/
29 File file = new File(parentPath, childrlPath);
30 /*传输创建到本地*/
31 try {
32 item.transferTo(file);
33 /*上传成功*/
34
35 } catch (IOException e) {
36 e.printStackTrace();
37 return "fileuploadmore";
38 }
39 }
40 }
41
42
43 /*上传失败*/
44 return "success";
45 }
46
47
48
49 /*最初始版本*/
50 @RequestMapping("/fileuploadfirst")
51 public String fileuploadfirst(MultipartFile upload, HttpSession session){
52 /*获取上传文件的简单名称例如 1.txt*/
53 String childrlPath = upload.getOriginalFilename();
54 /*获得一个真实路径*/
55 String parentPath = session.getServletContext().getRealPath("/day24/upload");
56 /*获取一个完整的文件对象*/
57 File file=new File(parentPath,childrlPath);
58 /*传输创建到本地*/
59 try {
60 upload.transferTo(file);
61 /*上传成功*/
62 return "success";
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66
67 /*上传失败*/
68 return "fileupload";
69 }
70}
71
3.自己的xml配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/mvc
9 http://www.springframework.org/schema/mvc/spring-mvc.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context.xsd">
12
13 <!--包扫描器-->
14 <context:component-scan base-package="cn.dawn.day24fileupload"></context:component-scan>
15 <!--视图解析器-->
16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17 <property name="prefix" value="/day24/"></property>
18 <property name="suffix" value=".jsp"></property>
19 </bean>
20
21 <!--多部分文件解析器-->
22 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
23 <!--文件名的编码-->
24 <property name="defaultEncoding" value="UTF-8"></property>
25 <!--限制文件大小--><!--这个单位是byte,我这儿限制的是20mb最大容量-->
26 <property name="maxUploadSize" value="20971520"></property>
27 </bean>
28
29 <!--绑定注解驱动-->
30 <mvc:annotation-driven></mvc:annotation-driven>
31
32</beans>
33
4.修改web.xml的中央调度器的上下文配置位置为上面那个xml
5.在webapp下你jsp页面的那个包下创建一个文件夹,我的叫upload,里面随便扔个东西,编译后,如果即使这样target目录下还没有upload这个文件夹的话,就手动创建
6.启动tomcat,访问fileuploadmore.jsp页面****