struts2文件上传.docx
《struts2文件上传.docx》由会员分享,可在线阅读,更多相关《struts2文件上传.docx(20页珍藏版)》请在冰点文库上搜索。
struts2文件上传
struts2文件上传
1.文件的上传和下载分为单个文件上传和多个文件上传
2.
3.一 单个的文件上传
4.
5..1.首先写一个jsp页面:
6.
7.
8.
9.
10.
11.
单个文件上传和多个文件上传
12.
13.
20.
21.
22.
23.
24.
25.
26.
27.2.访问到的struts.xml文件
28.
29.
xml version="1.0" encoding="GBK"?
>
30.
31.
DOCTYPE struts PUBLIC
32.
33. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
34.
35. "http:
//struts.apache.org/dtds/struts-2.3.dtd">
36.
37.
38.
39.
40.
41.
42.
43. ../success.jsp
44.
45.
46.
47.
48.
49.
50.
51.3.访问到UploadAction.java的action
52.
53.package cn.csdn.hr.up.action;
54.
55.
56.
57.import java.io.File;
58.
59.import java.io.IOException;
60.
61.
62.
63.import mons.io.FileUtils;
64.
65.import org.apache.struts2.ServletActionContext;
66.
67.
68.
69.import com.opensymphony.xwork2.ActionSupport;
70.
71.
72.
73.public class UploadAction extends ActionSupport {
74.
75. private static final long serialVersionUID = 1L;
76.
77.
78.
79. /**
80.
81. 都是规定,File的对象和input中的name(xxx)相同,文件类型和文件名都是xxxContextType,xxxFileName
82.
83. */
84.
85.
86.
87. // 得到上传文件的名称一定与name值一直
88.
89. private File upload;
90.
91. // 上传文件的类型 ContentType
92.
93. private String uploadContentType;
94.
95. // 上传文件的名称
96.
97. private String uploadFileName;
98.
99.
100.
101. public File getUpload() {
102.
103. return upload;
104.
105. }
106.
107.
108.
109. public void setUpload(File upload) {
110.
111. this.upload = upload;
112.
113. }
114.
115.
116.
117. public String getUploadContentType() {
118.
119. return uploadContentType;
120.
121. }
122.
123.
124.
125. public void setUploadContentType(String uploadContentType) {
126.
127. this.uploadContentType = uploadContentType;
128.
129. }
130.
131.
132.
133. public String getUploadFileName() {
134.
135. return uploadFileName;
136.
137. }
138.
139.
140.
141. public void setUploadFileName(String uploadFileName) {
142.
143. this.uploadFileName = uploadFileName;
144.
145. }
146.
147.
148.
149. public static long getSerialversionuid() {
150.
151. return serialVersionUID;
152.
153. }
154.
155.
156.
157. public String upload() {
158.
159. String path = ServletActionContext.getServletContext().getRealPath(
160.
161. "/upload");
162.
163. // 写到指定路径
164.
165. File file = new File(path);
166.
167. //判断指定的路径下是否有uplaod,如果没有,自动创建
168.
169. if (!
file.exists()) {
170.
171. file.mkdirs();
172.
173. }
174.
175. try {
176.
177. FileUtils.copyFile(upload, new File(file, uploadFileName));
178.
179. } catch (IOException e) {
180.
181. // TODO Auto-generated catch block
182.
183. e.printStackTrace();
184.
185. }
186.
187. return SUCCESS;
188.
189. }
190.
191.}
192.
193.二 多个文件的上传
194.
195.多个文件的上传不同的是在action中的获取到的是数组或者是list集合
196.
197.
198.
199.数组的action为:
200.
201.package cn.csdn.hr.up.action;
202.
203.
204.
205.import java.io.File;
206.
207.import java.io.IOException;
208.
209.
210.
211.import mons.io.FileUtils;
212.
213.import org.apache.struts2.ServletActionContext;
214.
215.
216.
217.import com.opensymphony.xwork2.ActionSupport;
218.
219.
220.
221.public class UploadsAction extends ActionSupport {
222.
223. private static final long serialVersionUID = 1L;
224.
225.
226.
227. // 得到上传文件的名称一定与name值一直
228.
229. private File upload[];
230.
231. // 上传文件的类型 ContentType
232.
233. private String uploadContentType[];
234.
235. // 上传文件的名称
236.
237. private String uploadFileName[];
238.
239.
240.
241. public File[] getUpload() {
242.
243. return upload;
244.
245. }
246.
247.
248.
249. public void setUpload(File[] upload) {
250.
251. this.upload = upload;
252.
253. }
254.
255.
256.
257. public String[] getUploadContentType() {
258.
259. return uploadContentType;
260.
261. }
262.
263.
264.
265. public void setUploadContentType(String[] uploadContentType) {
266.
267. this.uploadContentType = uploadContentType;
268.
269. }
270.
271.
272.
273. public String[] getUploadFileName() {
274.
275. return uploadFileName;
276.
277. }
278.
279.
280.
281. public void setUploadFileName(String[] uploadFileName) {
282.
283. this.uploadFileName = uploadFileName;
284.
285. }
286.
287.
288.
289. public static long getSerialversionuid() {
290.
291. return serialVersionUID;
292.
293. }
294.
295.
296.
297. public String uploads() {
298.
299. String path = ServletActionContext.getServletContext().getRealPath(
300.
301. "/upload");
302.
303.
304.
305. // 写到指定路径
306.
307. File file = new File(path);
308.
309. //判断指定的路径下是否有uplaod,如果没有,自动创建
310.
311. if (!
file.exists()) {
312.
313. file.mkdirs();
314.
315. }
316.
317. try {
318.
319. for(int i = 0;i320.
321. FileUtils.copyFile(upload[i], new File(file, uploadFileName[i]));
322.
323. }
324.
325. } catch (IOException e) {
326.
327. // TODO Auto-generated catch block
328.
329. e.printStackTrace();
330.
331. }
332.
333. System.out.println("上传文件的名称:
" + uploadFileName + "上传的路径:
" + path
334.
335. + "上传的类型:
" + uploadContentType);
336.
337. return SUCCESS;
338.
339. }
340.
341.}
342.
343.
344.
345.
346.
347.List集合的action为:
;
348.
349.
350.
351.
352.
353.
354.
355.package cn.csdn.hr.up.action;
356.
357.
358.
359.import java.io.File;
360.
361.import java.io.IOException;
362.
363.import java.util.List;
364.
365.
366.
367.import mons.io.FileUtils;
368.
369.import org.apache.struts2.ServletActionContext;
370.
371.
372.
373.import com.opensymphony.xwork2.ActionSupport;
374.
375.
376.
377.public class UploadListAction extends ActionSupport {
378.
379. private static final long serialVersionUID = 1L;
380.
381. // 得到上传文件的名称一定与name值一直
382.
383. private List upload;
384.
385. // 上传文件的类型 ContentType
386.
387. private List uploadContentType;
388.
389. // 上传文件的名称
390.
391. private List uploadFileName;
392.
393.
394.
395. public List getUpload() {
396.
397. return upload;
398.
399. }
400.
401.
402.
403. public void setUpload(List upload) {
404.
405. this.upload = upload;
406.
407. }
408.
409.
410.
411. public List getUploadContentType() {
412.
413. return uploadContentType;
414.
415. }
416.
417.
418.
419. public void setUploadContentType(List uploadContentType) {
420.
421. this.uploadContentType = uploadContentType;
422.
423. }
424.
425.
426.
427. public List getUploadFileName() {
428.
429. return uploadFileName;
430.
431. }
432.
433.
434.
435. public void setUploadFileName(List uploadFileName) {
436.
437. this.uploadFileName = uploadFileName;
438.
439. }
440.
441.
442.
443. public static long getSerialversionuid() {
444.
445. return serialVersionUID;
446.
447. }
448.
449.
450.
451. public String uploadList() {
452.
453. String path = ServletActionContext.getServletContext().getRealPath(
454.
455. "/upload");
456.
457.
458.
459. // 写到指定路径
460.
461. File file = new File(path);
462