KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > formbean > UploadImageForm


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.formbean;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.struts.action.ActionError;
25 import org.apache.struts.action.ActionErrors;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.upload.FormFile;
28
29 /**
30  * 用于上传图片的表单类
31  * @author Liudong
32  */

33 public class UploadImageForm extends DlogActionForm {
34     
35     FormFile uploadFile = null;
36     String JavaDoc imageUrl = null;
37     String JavaDoc displayText = null;
38     String JavaDoc txtWidth = null;
39     String JavaDoc txtHeight = null;
40     String JavaDoc txtBorder = null;
41     String JavaDoc txtAlign = "left";
42
43     /**
44      * 验证输入的有效性
45      */

46     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc req) {
47         ActionErrors aes = new ActionErrors();
48         try {
49             if(!StringUtils.isEmpty(txtWidth))
50                 Integer.parseInt(txtWidth);
51         }catch(Exception JavaDoc e) {
52             aes.add("txtWidth",new ActionError("illegal_input_value"));
53         }
54         try {
55             if(!StringUtils.isEmpty(txtHeight))
56                 Integer.parseInt(txtHeight);
57         }catch(Exception JavaDoc e) {
58             aes.add("txtHeight",new ActionError("illegal_input_value"));
59         }
60         try {
61             if(!StringUtils.isEmpty(txtBorder))
62                 Integer.parseInt(txtBorder);
63         }catch(Exception JavaDoc e) {
64             aes.add("txtBorder",new ActionError("illegal_input_value"));
65         }
66         if(uploadFile!=null){
67             if(uploadFile.getFileSize()>1000000)
68                 aes.add("uploadFile",new ActionError("upload_file_size_exceed"));
69             String JavaDoc fn = uploadFile.getFileName().toLowerCase();
70             int i=0;
71             for(;i<types.size();i++){
72                 if(fn.endsWith((String JavaDoc)types.get(i)))
73                     break;
74             }
75             if(i==types.size())
76                 aes.add("uploadFile",new ActionError("upload_file_extend_noallow"));
77             if(uploadFile.getFileSize()<=0)
78                 aes.add("uploadFile",new ActionError("upload_file_illegal"));
79         }
80         return aes;
81     }
82     public final static List JavaDoc types = Arrays.asList(new String JavaDoc[]{"gif","jpg","bmp","png"});
83     public static List JavaDoc getTypes() {
84         return types;
85     }
86     public String JavaDoc getDisplayText() {
87         return displayText;
88     }
89     public void setDisplayText(String JavaDoc displayText) {
90         this.displayText = displayText;
91     }
92     public String JavaDoc getImageUrl() {
93         return imageUrl;
94     }
95     public void setImageUrl(String JavaDoc imageUrl) {
96         this.imageUrl = imageUrl;
97     }
98     public String JavaDoc getTxtAlign() {
99         return txtAlign;
100     }
101     public void setTxtAlign(String JavaDoc txtAlign) {
102         this.txtAlign = txtAlign;
103     }
104     public String JavaDoc getTxtBorder() {
105         return txtBorder;
106     }
107     public void setTxtBorder(String JavaDoc txtBorder) {
108         this.txtBorder = txtBorder;
109     }
110     public String JavaDoc getTxtHeight() {
111         return txtHeight;
112     }
113     public void setTxtHeight(String JavaDoc txtHeight) {
114         this.txtHeight = txtHeight;
115     }
116     public String JavaDoc getTxtWidth() {
117         return txtWidth;
118     }
119     public void setTxtWidth(String JavaDoc txtWidth) {
120         this.txtWidth = txtWidth;
121     }
122     public FormFile getUploadFile() {
123         return uploadFile;
124     }
125     public void setUploadFile(FormFile uploadFile) {
126         this.uploadFile = uploadFile;
127     }
128 }
129
Popular Tags