View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts.webapp.upload;
23  
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  
31  import jakarta.servlet.http.HttpServletRequest;
32  import jakarta.servlet.http.HttpServletResponse;
33  
34  import org.apache.struts.action.Action;
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  import org.apache.struts.upload.FormFile;
39  
40  
41  
42  /**
43   * This class takes the UploadForm and retrieves the text value
44   * and file attributes and puts them in the request for the display.jsp
45   * page to display them
46   *
47   * @author Mike Schachter
48   * @version $Rev$ $Date$
49   */
50  
51  
52  public class UploadAction extends Action
53  {
54      private static final long serialVersionUID = 6492050366211798185L;
55  
56      public ActionForward execute(ActionMapping mapping,
57                                   ActionForm form,
58                                   HttpServletRequest request,
59                                   HttpServletResponse response)
60          throws Exception {
61  
62          if (form instanceof UploadForm) {
63  
64              //this line is here for when the input page is upload-utf8.jsp,
65              //it sets the correct character encoding for the response
66              String encoding = request.getCharacterEncoding();
67              if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
68              {
69                  response.setContentType("text/html; charset=utf-8");
70              }
71  
72              UploadForm theForm = (UploadForm) form;
73  
74              //retrieve the text data
75              String text = theForm.getTheText();
76  
77              //retrieve the query string value
78              String queryValue = theForm.getQueryParam();
79  
80              //retrieve the file representation
81              FormFile file = theForm.getTheFile();
82  
83              final int fileCount;
84  
85              // Following is to test fix for STR-3173
86              if (file == null) {
87                  final FormFile[] files = form.getMultipartRequestHandler().getFileElements().get("otherFile");
88                  fileCount = files.length;
89                  file = fileCount == 0 ? null : files[0];
90              } else {
91                  final FormFile[] files = form.getMultipartRequestHandler().getFileElements().get("theFile");
92                  fileCount = files.length;
93              }
94  
95              //retrieve the file name
96              String fileName= file.getFileName();
97  
98              //retrieve the content type
99              String contentType = file.getContentType();
100 
101             boolean writeFile = theForm.getWriteFile();
102 
103             //retrieve the file size
104             String size = (file.getFileLength() + " bytes");
105 
106             String data = null;
107 
108             try (InputStream stream = file.getInputStream()) {
109                 //retrieve the file data
110                 if (!writeFile) {
111                     //only write files out that are less than 4MB
112                     if (file.getFileLength() < (4*1024000)) {
113 
114                         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
115                             byte[] buffer = new byte[8192];
116                             int bytesRead = 0;
117                             while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
118                                 baos.write(buffer, 0, bytesRead);
119                             }
120                             data = new String(baos.toByteArray());
121                         }
122                     } else {
123                         data = new String("The file is greater than 4MB, " +
124                                 " and has not been written to stream." +
125                                 " File Size: " + file.getFileLength() + " bytes. This is a" +
126                                 " limitation of this particular web application, hard-coded" +
127                                 " in org.apache.struts.webapp.upload.UploadAction");
128                     }
129                 } else {
130                     //write the file to the file specified
131                     try (OutputStream bos = new FileOutputStream(theForm.getFilePath())) {
132                         int bytesRead = 0;
133                         byte[] buffer = new byte[8192];
134                         while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
135                             bos.write(buffer, 0, bytesRead);
136                         }
137                     }
138                     data = "The file has been written to \"" + theForm.getFilePath() + "\"";
139                 }
140             } catch (IOException e) {
141                 return null;
142             }
143 
144             //place the data into the request for retrieval from display.jsp
145             request.setAttribute("text", text);
146             request.setAttribute("queryValue", queryValue);
147             request.setAttribute("fileCount", fileCount);
148             request.setAttribute("fileName", fileName);
149             request.setAttribute("contentType", contentType);
150             request.setAttribute("size", size);
151             request.setAttribute("data", data);
152 
153             //destroy the temporary file created
154             file.destroy();
155 
156             //return a forward to display.jsp
157             return mapping.findForward("display");
158         }
159 
160         //this shouldn't happen in this example
161         return null;
162     }
163 }