View Javadoc

1   /*
2    * $Id: UploadAction.java 421488 2006-07-13 03:43:08Z wsmoak $
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;
23  import java.io.FileNotFoundException;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
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: 421488 $ $Date: 2006-07-12 20:43:08 -0700 (Wed, 12 Jul 2006) $
47   */
48  
49  
50  public class UploadAction extends Action
51  {
52      public ActionForward execute(ActionMapping mapping,
53                                   ActionForm form,
54                                   HttpServletRequest request,
55                                   HttpServletResponse response)
56          throws Exception {
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 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 text = theForm.getTheText();
72  
73              //retrieve the query string value
74              String queryValue = theForm.getQueryParam();
75  
76              //retrieve the file representation
77              FormFile file = theForm.getTheFile();
78  
79              //retrieve the file name
80              String fileName= file.getFileName();
81  
82              //retrieve the content type
83              String contentType = file.getContentType();
84  
85              boolean writeFile = theForm.getWriteFile();
86  
87              //retrieve the file size
88              String size = (file.getFileSize() + " bytes");
89  
90              String data = null;
91  
92              try {
93                  //retrieve the file data
94                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
95                  InputStream 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(baos.toByteArray());
106                     }
107                     else {
108                         data = new String("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 bos = new FileOutputStream(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 fnfe) {
130                 return null;
131             }
132             catch (IOException 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 }