KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > file > MultiImageForm


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.strutsutil.file;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19
20 import org.apache.struts.action.ActionMessage;
21 import org.apache.struts.action.ActionErrors;
22 import org.apache.struts.action.ActionForm;
23 import org.apache.struts.action.ActionMapping;
24 import org.apache.struts.upload.FormFile;
25 import org.apache.struts.upload.MultipartRequestHandler;
26
27 import java.util.*;
28
29 /**
30  * 一次性上传多个图片Form
31  *
32  * å?¯ä»¥Override本ActionForm,设计自己的UploadImageForm
33  *
34  * 1. é…?置如下
35  * <form-bean name="uploadForm" type="com.jdon.strutsutil.file.UploadImageForm" />
36  * 2 .注æ„?,需è¦?é…?置上传文件大å°?é™?制,在struts-config.xml有:
37  * <controller maxFileSize="100K" />
38  * 3. é…?ç½®ApplicationResoureces
39  * maxLengthExceeded=The maximum upload length has been exceeded by the client.
40  * notImage=this is not Image.
41  *
42  * <p>Copyright: Jdon.com Copyright (c) 2003</p>
43  * <p></p>
44  * @author banq
45  * @version 1.0
46  */

47 public class MultiImageForm extends ActionForm {
48   public final static String JavaDoc module = MultiImageForm.class.getName();
49
50   /** æ¯?张图片大å°?,由 struts-config.xml maxFileSize 设置 */
51   private static String JavaDoc ERROR_PROPERTY_MAX_LENGTH_EXCEEDED =
52       "com.jdon.strutsutil.file.MaxLengthExceeded";
53
54   /** 最多图片数 是2 */
55   public static int MAX_IMAGES_COUNT = 2;
56
57
58   private Map fileMap = new HashMap();
59   private Map nameMap = new HashMap();
60
61   public FormFile getFile( int index ){
62       return (FormFile) fileMap.get( new Integer JavaDoc( index ) );
63   }
64
65   public void setFile(int index, FormFile file){
66     fileMap.put(new Integer JavaDoc(index), file);
67   }
68
69   public FormFile[] getFiles(){
70       return (FormFile[])fileMap.values().toArray(new FormFile[fileMap.size()]);
71   }
72
73   public String JavaDoc getName( int index ){
74       return (String JavaDoc)nameMap.get( new Integer JavaDoc( index ) );
75   }
76
77   public void setName(int index, String JavaDoc name){
78     nameMap.put(new Integer JavaDoc(index), name);
79   }
80
81   public String JavaDoc[] getNames(){
82       return (String JavaDoc[])nameMap.values().toArray(new String JavaDoc[0]);
83   }
84
85
86
87
88   /**
89    * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
90    * validate method.
91    */

92   public ActionErrors validate(ActionMapping mapping,
93                                HttpServletRequest JavaDoc request) {
94
95     ActionErrors errors = null;
96     //has the maximum length been exceeded?
97
Boolean JavaDoc maxLengthExceeded = (Boolean JavaDoc)
98         request.getAttribute(MultipartRequestHandler.
99                              ATTRIBUTE_MAX_LENGTH_EXCEEDED);
100     if ( (maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
101       errors = new ActionErrors();
102       errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED,
103                  new ActionMessage("maxLengthExceeded"));
104     }else if (fileMap.size()> MAX_IMAGES_COUNT){
105       errors = new ActionErrors();
106       errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED,
107                  new ActionMessage("maxLengthExceeded"));
108     }else{
109       //retrieve the file name
110
Iterator iter = fileMap.values().iterator();
111       while(iter.hasNext()){
112         FormFile file = (FormFile)iter.next();
113         String JavaDoc fileName = file.getFileName();
114         if ( (!fileName.toLowerCase().endsWith(".gif")) &&
115             ! (fileName.toLowerCase().endsWith(".jpg")) &&
116             ! (fileName.toLowerCase().endsWith(".png"))) {
117           errors = new ActionErrors();
118           errors.add("notImage", new ActionMessage("notImage"));
119         }
120       }
121     }
122     return errors;
123   }
124
125 }
126
Popular Tags