web.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
34
35
36
37 |
<span style="font-family:SimSun;font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>FileUploadAndDownload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 上传配置 -->
<servlet>
<servlet-name>uploadHandleServlet</servlet-name>
<servlet-class>com.zeng.controller.UploadHandleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadHandleServlet</servlet-name>
<url-pattern>/upload/uploadHandleServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>listFileServlet</servlet-name>
<servlet-class>com.zeng.controller.ListFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>listFileServlet</servlet-name>
<url-pattern>/listFileServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>downLoadServlet</servlet-name>
<servlet-class>com.zeng.controller.DownLoadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>downLoadServlet</servlet-name>
<url-pattern>/download/downLoadServlet</url-pattern>
</servlet-mapping>
</web-app></span> |
2.upload.jsp文件
?
|
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 |
<span style="font-family:SimSun;font-size:14px;"><%@page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<!--
${pageContext.request.contextPath} : 作用是取出部署的应用程序名
缺点:操作不便,其他工具无法正确解释${pageContext.request.contextPath}
如果Servlet的配置路径为 /upload/uploadHandleServlet
action跳转路径为: ${pageContext.request.contextPath}/upload/uploadHandleServlet
上传文件时,必须
1.enctype="multipart/form-data"
2.method=post
-->
<form action="${pageContext.request.contextPath}/upload/uploadHandleServlet" enctype="multipart/form-data" method="post">
上传用户:<input type="text" name="username"><br/>
上传文件1:<input type="file" name="file1"><br/>
上传文件2:<input type="file" name="file2"><br/>
<input type="submit" value="提交">
</form>
</body>
</html></span> |
3.message.jsp
?
|
1
2
3
4
5
6
7
8
9
10 |
<span style="font-family:SimSun;font-size:14px;"><%@page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>消息提示</title>
</head>
<body>
${message}
</body>
</html></span> |
4.UploadHandleServlet.java
?
|
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
