1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
44
45
46
47
48
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
65
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
75 String text = theForm.getTheText();
76
77
78 String queryValue = theForm.getQueryParam();
79
80
81 FormFile file = theForm.getTheFile();
82
83 final int fileCount;
84
85
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
96 String fileName= file.getFileName();
97
98
99 String contentType = file.getContentType();
100
101 boolean writeFile = theForm.getWriteFile();
102
103
104 String size = (file.getFileLength() + " bytes");
105
106 String data = null;
107
108 try (InputStream stream = file.getInputStream()) {
109
110 if (!writeFile) {
111
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
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
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
154 file.destroy();
155
156
157 return mapping.findForward("display");
158 }
159
160
161 return null;
162 }
163 }