KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > upload > UploadForm


1 /*
2  * $Id: UploadForm.java 54929 2004-10-16 16:38:42Z germuska $
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 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import org.apache.struts.action.*;
24 import org.apache.struts.upload.FormFile;
25 import org.apache.struts.upload.MultipartRequestHandler;
26
27 /**
28  * This class is a placeholder for form values. In a multipart request, files are represented by
29  * set and get methods that use the class org.apache.struts.upload.FormFile, an interface with
30  * basic methods to retrieve file information. The actual structure of the FormFile is dependant
31  * on the underlying impelementation of multipart request handling. The default implementation
32  * that struts uses is org.apache.struts.upload.CommonsMultipartRequestHandler.
33  *
34  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
35  */

36 public class UploadForm extends ActionForm {
37     
38     /**
39      * The value of the text the user has sent as form data
40      */

41     protected String JavaDoc theText;
42
43     /**
44      * The value of the embedded query string parameter
45      */

46     protected String JavaDoc queryParam;
47
48     /**
49      * Whether or not to write to a file
50      */

51     protected boolean writeFile;
52
53     /**
54      * The file that the user has uploaded
55      */

56     protected FormFile theFile;
57
58     /**
59      * The file path to write to
60      */

61     protected String JavaDoc filePath;
62
63     /**
64      * Retrieve the value of the text the user has sent as form data
65      */

66     public String JavaDoc getTheText() {
67         return theText;
68     }
69
70     /**
71      * Set the value of the form data text
72      */

73     public void setTheText(String JavaDoc theText) {
74         this.theText = theText;
75     }
76
77     /**
78      * Retrieve the value of the query string parameter
79      */

80     public String JavaDoc getQueryParam() {
81         return queryParam;
82     }
83
84     /**
85      * Set the value of the query string parameter
86      */

87     public void setQueryParam(String JavaDoc queryParam) {
88         this.queryParam = queryParam;
89     }
90
91     /**
92      * Retrieve a representation of the file the user has uploaded
93      */

94     public FormFile getTheFile() {
95         return theFile;
96     }
97
98     /**
99      * Set a representation of the file the user has uploaded
100      */

101     public void setTheFile(FormFile theFile) {
102         this.theFile = theFile;
103     }
104
105     /**
106      * Set whether or not to write to a file
107      */

108     public void setWriteFile(boolean writeFile) {
109         this.writeFile = writeFile;
110     }
111
112     /**
113      * Get whether or not to write to a file
114      */

115     public boolean getWriteFile() {
116         return writeFile;
117     }
118
119     /**
120      * Set the path to write a file to
121      */

122     public void setFilePath(String JavaDoc filePath) {
123         this.filePath = filePath;
124     }
125
126     /**
127      * Get the path to write a file to
128      */

129     public String JavaDoc getFilePath() {
130         return filePath;
131     }
132
133     public void reset() {
134         writeFile = false;
135     }
136
137     /**
138      * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
139      * validate method.
140      */

141     public ActionErrors validate(
142         ActionMapping mapping,
143         HttpServletRequest JavaDoc request) {
144             
145         ActionErrors errors = null;
146         //has the maximum length been exceeded?
147
Boolean JavaDoc maxLengthExceeded =
148             (Boolean JavaDoc) request.getAttribute(
149                 MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
150                 
151         if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
152             errors = new ActionErrors();
153             errors.add(
154                 ActionMessages.GLOBAL_MESSAGE ,
155                 new ActionMessage("maxLengthExceeded"));
156             errors.add(
157                 ActionMessages.GLOBAL_MESSAGE ,
158                 new ActionMessage("maxLengthExplanation"));
159         }
160         return errors;
161
162     }
163 }
164
Popular Tags