KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > file > MultiUploadAction


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.strutsutil.file;
17
18 import java.io.*;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import org.apache.struts.action.Action;
22 import org.apache.struts.action.ActionForm;
23 import org.apache.struts.action.ActionForward;
24 import org.apache.struts.action.ActionMapping;
25 import org.apache.struts.upload.FormFile;
26
27 import com.jdon.strutsutil.file.filter.*;
28
29 /**
30  * 无需继承实现 直接é…?置使用
31  * 使用本上传Action时,需è¦?在struts_config.xmlé…?ç½®
32  * <action name="uploadForm" type="com.jdon.strutsutil.file.UploadAction" input="/admin/uploadImg.jsp" scope="request" path="/admin/uploadAction">
33  * <forward name="display" path="/admin/uploadOk.jsp" />
34  * </action>
35  *
36  * <p>Copyright: Jdon.com Copyright (c) 2003</p>
37  * <p></p>
38  * @author banq
39  * @version 1.0
40  */

41 public class MultiUploadAction extends Action {
42
43   public ActionForward execute(ActionMapping mapping,
44                                ActionForm form,
45                                HttpServletRequest JavaDoc request,
46                                HttpServletResponse JavaDoc response) throws Exception JavaDoc {
47
48     if (form == null) {
49       throw new Exception JavaDoc("please setup UploadImageForm in struts-config.xml");
50     }
51     MultiImageForm theForm = (MultiImageForm) form;
52     FormFile[] files = theForm.getFiles();
53     int length = files.length;
54
55     ImageSessionFilter imageFilter = new ImageSessionFilter();
56
57     UploadFile uploadFile = null;
58     for (int i = 0; i < length; i++) {
59       uploadFile = doFormFile(request, files[i]);
60       uploadFile.setName(theForm.getNames()[i]);
61       //ä¿?存到HttpSession中
62
imageFilter.addUploadFile(request, uploadFile);
63     }
64
65     //return a forward to display.jsp
66
return mapping.findForward("display");
67
68   }
69
70   private UploadFile doFormFile(HttpServletRequest JavaDoc request, FormFile file) throws
71       Exception JavaDoc {
72     UploadFile uploadFile = new UploadFile();
73     try {
74       //retrieve the file data
75
ByteArrayOutputStream baos = new ByteArrayOutputStream();
76       InputStream stream = file.getInputStream();
77
78       byte[] buffer = new byte[8192];
79       int bytesRead = 0;
80       while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
81         baos.write(buffer, 0, bytesRead);
82       }
83
84       byte[] data = baos.toByteArray();
85       baos.close();
86       //close the stream
87
stream.close();
88
89       if (data != null) {
90         uploadFile.setData(data);
91         uploadFile.setContentType(file.getContentType());
92         uploadFile.setSize(file.getFileSize());
93
94       }
95     } catch (Exception JavaDoc ex) {
96       throw new Exception JavaDoc(ex);
97     } finally {
98       //destroy the temporary file created
99
file.destroy();
100     }
101
102     return uploadFile;
103   }
104
105 }
106
Popular Tags