KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > form > UploadForm


1 package org.appfuse.webapp.form;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4
5 import org.apache.struts.action.ActionErrors;
6 import org.apache.struts.action.ActionMapping;
7 import org.apache.struts.action.ActionMessage;
8 import org.apache.struts.upload.FormFile;
9 import org.apache.struts.upload.MultipartRequestHandler;
10
11
12 /**
13  * This class is modeled after the UploadForm from the struts-upload example
14  * application. For more information on implementation details, please
15  * see that application.
16  *
17  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
18  * @version $Revision: 1.5.2.1 $ $Date: 2006-07-09 13:26:51 -0600 (Sun, 09 Jul 2006) $
19  *
20  * @struts.form name="uploadForm"
21  */

22 public class UploadForm extends BaseForm {
23     private static final long serialVersionUID = 3257850969634190134L;
24
25     public static final String JavaDoc ERROR_PROPERTY_MAX_LENGTH_EXCEEDED =
26         "MaxLengthExceeded";
27
28     /** The value of the text the user has sent as form data */
29     protected String JavaDoc name;
30
31     /** The file that the user has uploaded */
32     protected FormFile file;
33
34     /**
35      * Retrieve the name the user has given the uploaded file
36      *
37      * @return the file's name
38      */

39     public String JavaDoc getName() {
40         return name;
41     }
42
43     /**
44      * Set the name of the uploaded file (by the user)
45      *
46      * @param name
47      */

48     public void setName(String JavaDoc name) {
49         this.name = name;
50     }
51
52     /**
53      * Retrieve a representation of the file the user has uploaded
54      *
55      * @return FormFile the uploaded file
56      */

57     public FormFile getFile() {
58         return file;
59     }
60
61     /**
62      * Set a representation of the file the user has uploaded
63      *
64      * @param file the file to upload
65      */

66     public void setFile(FormFile file) {
67         this.file = file;
68     }
69
70     /**
71      * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
72      * validate method.
73      */

74      public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
75          
76          ActionErrors errors = super.validate(mapping, request);
77          
78          // has the maximum length been exceeded?
79
Boolean JavaDoc maxLengthExceeded =
80              (Boolean JavaDoc) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
81          if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
82              if (errors == null) {
83                  errors = new ActionErrors();
84              }
85              errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionMessage("maxLengthExceeded"));
86          }
87          
88          return errors;
89      }
90 }
91
Popular Tags