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.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.struts.action.Action;
36 import org.apache.struts.action.ActionForm;
37 import org.apache.struts.action.ActionForward;
38 import org.apache.struts.action.ActionMapping;
39 import org.apache.struts.upload.FormFile;
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class UploadAction extends Action
54 {
55 public ActionForward execute(ActionMapping mapping,
56 ActionForm form,
57 HttpServletRequest request,
58 HttpServletResponse response)
59 throws Exception {
60
61 if (form instanceof UploadForm) {
62
63
64
65 String encoding = request.getCharacterEncoding();
66 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
67 {
68 response.setContentType("text/html; charset=utf-8");
69 }
70
71 UploadForm theForm = (UploadForm) form;
72
73
74 String text = theForm.getTheText();
75
76
77 String queryValue = theForm.getQueryParam();
78
79
80 FormFile file = theForm.getTheFile();
81
82
83 String fileName= file.getFileName();
84
85
86 String contentType = file.getContentType();
87
88 boolean writeFile = theForm.getWriteFile();
89
90
91 String size = (file.getFileSize() + " bytes");
92
93 String data = null;
94
95 try {
96
97 ByteArrayOutputStream baos = new ByteArrayOutputStream();
98 InputStream stream = file.getInputStream();
99 if (!writeFile) {
100
101 if (file.getFileSize() < (4*1024000)) {
102
103 byte[] buffer = new byte[8192];
104 int bytesRead = 0;
105 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
106 baos.write(buffer, 0, bytesRead);
107 }
108 data = new String(baos.toByteArray());
109 }
110 else {
111 data = new String("The file is greater than 4MB, " +
112 " and has not been written to stream." +
113 " File Size: " + file.getFileSize() + " bytes. This is a" +
114 " limitation of this particular web application, hard-coded" +
115 " in org.apache.struts.webapp.upload.UploadAction");
116 }
117 }
118 else {
119
120 OutputStream bos = new FileOutputStream(theForm.getFilePath());
121 int bytesRead = 0;
122 byte[] buffer = new byte[8192];
123 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
124 bos.write(buffer, 0, bytesRead);
125 }
126 bos.close();
127 data = "The file has been written to \"" + theForm.getFilePath() + "\"";
128 }
129
130 stream.close();
131 }
132 catch (FileNotFoundException fnfe) {
133 return null;
134 }
135 catch (IOException ioe) {
136 return null;
137 }
138
139
140 request.setAttribute("text", text);
141 request.setAttribute("queryValue", queryValue);
142 request.setAttribute("fileName", fileName);
143 request.setAttribute("contentType", contentType);
144 request.setAttribute("size", size);
145 request.setAttribute("data", data);
146
147
148 file.destroy();
149
150
151 return mapping.findForward("display");
152 }
153
154
155 return null;
156 }
157 }