KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > upload > UploadAction


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

18
19 package org.apache.struts.webapp.upload;
20
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.struts.action.Action;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.upload.FormFile;
37
38
39
40 /**
41  * This class takes the UploadForm and retrieves the text value
42  * and file attributes and puts them in the request for the display.jsp
43  * page to display them
44  *
45  * @author Mike Schachter
46  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
47  */

48
49
50 public class UploadAction extends Action
51 {
52     public ActionForward execute(ActionMapping mapping,
53                                  ActionForm form,
54                                  HttpServletRequest JavaDoc request,
55                                  HttpServletResponse JavaDoc response)
56         throws Exception JavaDoc {
57
58         if (form instanceof UploadForm) {
59
60             //this line is here for when the input page is upload-utf8.jsp,
61
//it sets the correct character encoding for the response
62
String JavaDoc encoding = request.getCharacterEncoding();
63             if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
64             {
65                 response.setContentType("text/html; charset=utf-8");
66             }
67
68             UploadForm theForm = (UploadForm) form;
69
70             //retrieve the text data
71
String JavaDoc text = theForm.getTheText();
72
73             //retrieve the query string value
74
String JavaDoc queryValue = theForm.getQueryParam();
75
76             //retrieve the file representation
77
FormFile file = theForm.getTheFile();
78
79             //retrieve the file name
80
String JavaDoc fileName= file.getFileName();
81
82             //retrieve the content type
83
String JavaDoc contentType = file.getContentType();
84
85             boolean writeFile = theForm.getWriteFile();
86
87             //retrieve the file size
88
String JavaDoc size = (file.getFileSize() + " bytes");
89
90             String JavaDoc data = null;
91
92             try {
93                 //retrieve the file data
94
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
95                 InputStream JavaDoc stream = file.getInputStream();
96                 if (!writeFile) {
97                     //only write files out that are less than 1MB
98
if (file.getFileSize() < (4*1024000)) {
99
100                         byte[] buffer = new byte[8192];
101                         int bytesRead = 0;
102                         while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
103                             baos.write(buffer, 0, bytesRead);
104                         }
105                         data = new String JavaDoc(baos.toByteArray());
106                     }
107                     else {
108                         data = new String JavaDoc("The file is greater than 4MB, " +
109                                 " and has not been written to stream." +
110                                 " File Size: " + file.getFileSize() + " bytes. This is a" +
111                                 " limitation of this particular web application, hard-coded" +
112                                 " in org.apache.struts.webapp.upload.UploadAction");
113                     }
114                 }
115                 else {
116                     //write the file to the file specified
117
OutputStream JavaDoc bos = new FileOutputStream JavaDoc(theForm.getFilePath());
118                     int bytesRead = 0;
119                     byte[] buffer = new byte[8192];
120                     while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
121                         bos.write(buffer, 0, bytesRead);
122                     }
123                     bos.close();
124                     data = "The file has been written to \"" + theForm.getFilePath() + "\"";
125                 }
126                 //close the stream
127
stream.close();
128             }
129             catch (FileNotFoundException JavaDoc fnfe) {
130                 return null;
131             }
132             catch (IOException JavaDoc ioe) {
133                 return null;
134             }
135
136             //place the data into the request for retrieval from display.jsp
137
request.setAttribute("text", text);
138             request.setAttribute("queryValue", queryValue);
139             request.setAttribute("fileName", fileName);
140             request.setAttribute("contentType", contentType);
141             request.setAttribute("size", size);
142             request.setAttribute("data", data);
143
144             //destroy the temporary file created
145
file.destroy();
146
147             //return a forward to display.jsp
148
return mapping.findForward("display");
149         }
150
151         //this shouldn't happen in this example
152
return null;
153     }
154 }
Popular Tags