1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
61
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
71 String text = theForm.getTheText();
72
73
74 String queryValue = theForm.getQueryParam();
75
76
77 FormFile file = theForm.getTheFile();
78
79
80 String fileName= file.getFileName();
81
82
83 String contentType = file.getContentType();
84
85 boolean writeFile = theForm.getWriteFile();
86
87
88 String size = (file.getFileSize() + " bytes");
89
90 String data = null;
91
92 try {
93
94 ByteArrayOutputStream baos = new ByteArrayOutputStream();
95 InputStream stream = file.getInputStream();
96 if (!writeFile) {
97
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
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
127 stream.close();
128 }
129 catch (FileNotFoundException fnfe) {
130 return null;
131 }
132 catch (IOException ioe) {
133 return null;
134 }
135
136
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
145 file.destroy();
146
147
148 return mapping.findForward("display");
149 }
150
151
152 return null;
153 }
154 }