1 15 16 package com.jdon.strutsutil.file; 17 18 import javax.servlet.http.HttpServletRequest ; 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 47 public class MultiImageForm extends ActionForm { 48 public final static String module = MultiImageForm.class.getName(); 49 50 51 private static String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = 52 "com.jdon.strutsutil.file.MaxLengthExceeded"; 53 54 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 ( index ) ); 63 } 64 65 public void setFile(int index, FormFile file){ 66 fileMap.put(new Integer (index), file); 67 } 68 69 public FormFile[] getFiles(){ 70 return (FormFile[])fileMap.values().toArray(new FormFile[fileMap.size()]); 71 } 72 73 public String getName( int index ){ 74 return (String )nameMap.get( new Integer ( index ) ); 75 } 76 77 public void setName(int index, String name){ 78 nameMap.put(new Integer (index), name); 79 } 80 81 public String [] getNames(){ 82 return (String [])nameMap.values().toArray(new String [0]); 83 } 84 85 86 87 88 92 public ActionErrors validate(ActionMapping mapping, 93 HttpServletRequest request) { 94 95 ActionErrors errors = null; 96 Boolean maxLengthExceeded = (Boolean ) 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 Iterator iter = fileMap.values().iterator(); 111 while(iter.hasNext()){ 112 FormFile file = (FormFile)iter.next(); 113 String 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 |