1
2
3
4
5
6 package example.struts;
7
8 import org.apache.struts.action.ActionForm;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.action.ActionErrors;
11 import org.apache.struts.action.ActionMessage;
12 import org.apache.struts.upload.FormFile;
13 import javax.servlet.http.HttpServletRequest;
14
15 /***
16 *
17 * @author Sean C. Sullivan
18 *
19 */
20 public class FileUploadForm extends ActionForm
21 {
22 private FormFile file;
23
24 public void setFile(FormFile f)
25 {
26 this.file = f;
27 }
28
29 public FormFile getFile()
30 {
31 return this.file;
32 }
33
34 public void reset(ActionMapping mapping, HttpServletRequest request)
35 {
36 this.file = null;
37 }
38
39 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
40 {
41 ActionErrors errors = new ActionErrors();
42
43 if (this.getFile() == null)
44 {
45 errors.add("file", new ActionMessage("error.file.missing"));
46 }
47 else if (this.getFile().getFileName() == null)
48 {
49 errors.add("file", new ActionMessage("error.empty.filename"));
50 }
51 else if (this.getFile().getFileName().length() < 1)
52 {
53 errors.add("file", new ActionMessage("error.empty.filename"));
54 }
55 else if (this.getFile().getFileSize() < 1)
56 {
57 errors.add("file", new ActionMessage("error.file.size"));
58 }
59 return errors;
60 }
61
62 public String toString()
63 {
64 return "file=" + String.valueOf(this.getFile());
65 }
66 }