在前面我已经介绍过了利用Struts 2控制单文件上传:Struts 2读书笔记-----使用Struts 2控制文件上传 .其实对于多文件上传也差不多。加入我们要同时控制三个文件进行上传。那么页面得有三个文件上传域。在这里主要介绍采用数字和list来控制文件上传。
一、利用数组
利用数组来封装3个文件域。为了让数组一次性封装三个文件,我们需要将三个文件域的name属性设置为一样的。如下:
1. <form action="upload.action" method="POST" enctype="multipart/form-data">
2. 文件标题:<input type="text" name="title"/><Br />
3. 第一个文件:<input type="file" name="upload"/><br/>
4. 第二个文件:<input type="file" name="upload"/><br/>
5. 第三个文件:<input type="file" name="upload"/><br/>
6. <input type="submit" value="上传"/>
7. </form>
在处理上传的过程中,我们需要3个数组来分别封装文件名、文件类型、文件内容
1. public class UploadToArrayAction extends ActionSupport {
2. private String title; // 文件标题
3. private File[] upload; // 使用file数组封装多个文件域对应的文件内容
4. private String[] uploadContentType; // 使用字符串数组封装多个文件域对应的文件类型
5. private String[] uploadFileName; // 使用字符串数组封装对个文件域对应的文件名
6.
7. private String savePath; // 动态设置文件属性
8.
9. public String getTitle() {
10. return title;
11. }
12.
13. public void setTitle(String title) {
14. this.title = title;
15. }
16.
17. public File[] getUpload() {
18. return upload;
19. }
20.
21. public void setUpload(File[] upload) {
22. this.upload = upload;
23. }
24.
25. public String[] getUploadContentType() {
26. return uploadContentType;
27. }
28.
29. public void setUploadContentType(String[] uploadContentType) {
30. this.uploadContentType = uploadContentType;
31. }
32.
33. public String[] getUploadFileName() {
34. return uploadFileName;
35. }
36.
37. public void setUploadFileName(String[] uploadFileName) {
38. this.uploadFileName = uploadFileName;
39. }
40.
41. public String getSavePath() {
42. return savePath;
43. }
44.
45. public void setSavePath(String savePath) {
46. this.savePath = savePath;
47. }
48.
49. public String execute() throws Exception{
50. //取得需要上传的文件数组
51. File[] files = getUpload();
52. //遍历每个需要上传的文件
53. for (int i = 0; i < files.length; i++) {
54. //以服务器的文件保存地址和源文件名建立上传文件输出流
55. String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
56. FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName()[i]);
57. //以每个需要上传的文件建立输入流
58. FileInputStream fis = new FileInputStream(files[i]);
59. //将每个需要上传的文件写入
60. byte[] buffer = new byte[1024];
61. int length = 0;
62. while ((length=fis.read(buffer))>0) {
63. fos.write(buffer,0,length);
64. }
65. fos.close();
66. }
67. return SUCCESS;
68. }
struts.xml配置文件的配置
1. <package name="mystruts" extends="struts-default">
2. <action name="upload" class="com.app.action.UploadToArrayAction">
3. <!-- 动态设置Action的savePath属性 -->
4. <param name="savePath">/upload</param>
5. <!-- 配置文件上传的拦截器 -->
6. <interceptor-ref name="fileUpload">
7. <!-- 配置允许上传的文件类型 -->
8. <param name="allowedTypes">image/png,image/gif,image/jpeg</param>
9. <param name="maximumSize">20480000</param>
10. </interceptor-ref>
11.
12. <!-- 配置系统默认的拦截器 -->
13. <interceptor-ref name="defaultStack"></interceptor-ref>
14. <result name="success">/success.jsp</result>
15. <result name="input">/upload.jsp</result>
16. </action>
17. </package>
配置这个Action与配置单文件上传的Action其实是没有什么其他的区别的。
二、利用List
其实利用List和利用数组时没有什么区别的。只是将Array数组改为了泛型了list而已了。
1. public class UploadToListAction extends ActionSupport {
2. private String title; // 文件标题
3. private List<File> upload; // 使用file数组封装多个文件域对应的文件内容
4. private List<String> uploadContentType; // 使用字符串数组封装多个文件域对应的文件类型
5. private List<String> uploadFileName; // 使用字符串数组封装对个文件域对应的文件名
6.
7. private String savePath; // 动态设置文件属性
8.
9. public String getTitle() {
10. return title;
11. }
12.
13. public void setTitle(String title) {
14. this.title = title;
15. }
16.
17. public List<File> getUpload() {
18. return upload;
19. }
20.
21. public void setUpload(List<File> upload) {
22. this.upload = upload;
23. }
24.
25. public List<String> getUploadContentType() {
26. return uploadContentType;
27. }
28.
29. public void setUploadContentType(List<String> uploadContentType) {
30. this.uploadContentType = uploadContentType;
31. }
32.
33. public List<String> getUploadFileName() {
34. return uploadFileName;
35. }
36.
37. public void setUploadFileName(List<String> uploadFileName) {
38. this.uploadFileName = uploadFileName;
39. }
40.
41. public String getSavePath() {
42. return savePath;
43. }
44.
45. public void setSavePath(String savePath) {
46. this.savePath = savePath;
47. }
48.
49. public String execute() throws Exception {
50. // 取得需要上传的文件数组
51. List<File> files = getUpload();
52. // 遍历每个需要上传的文件
53. for (int i = 0; i < files.size(); i++) {
54. String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
55. FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName().get(i));
56. FileInputStream fis = new FileInputStream(files.get(i));
57. byte[] buffer = new byte[1024];
58. int length = 0;
59. while((length=fis.read())>0){
60. fos.write(buffer,0,length);
61. }
62. fos.close();
63. }
64. return SUCCESS;
65. }
66. }