1 5 package com.opensymphony.webwork.dispatcher.multipart; 6 7 import org.apache.commons.fileupload.DefaultFileItem; 8 import org.apache.commons.fileupload.DiskFileUpload; 9 import org.apache.commons.fileupload.FileItem; 10 import org.apache.commons.fileupload.FileUploadException; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.*; 16 17 22 public class JakartaMultiPartRequest extends MultiPartRequest { 23 private Map files = new HashMap(); 25 private Map params = new HashMap(); 27 private List errors = new ArrayList(); 29 30 38 public JakartaMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize) 39 throws IOException { 40 DiskFileUpload upload = new DiskFileUpload(); 41 upload.setSizeThreshold(0); 44 upload.setSizeMax(maxSize); 45 upload.setHeaderEncoding(servletRequest.getCharacterEncoding()); 46 if (saveDir != null) { 47 upload.setRepositoryPath(saveDir); 48 } 49 50 try { 52 List items = upload.parseRequest(servletRequest); 53 54 for (int i = 0; i < items.size(); i++) { 55 FileItem item = (FileItem) items.get(i); 56 log.debug("Found item " + item.getFieldName()); 57 if (item.isFormField()) { 58 log.debug("Item is a normal form field"); 59 List values; 60 if (params.get(item.getFieldName()) != null) { 61 values = (List) params.get(item.getFieldName()); 62 } else { 63 values = new ArrayList(); 64 } 65 66 String charset = servletRequest.getCharacterEncoding(); 71 if (charset != null) { 72 values.add(item.getString(charset)); 73 } else { 74 values.add(item.getString()); 75 } 76 params.put(item.getFieldName(), values); 77 } else if (item.getSize() == 0) { 78 log.debug("Item is a file upload of 0 size, ignoring"); 79 } else { 80 log.debug("Item is a file upload"); 81 82 List values; 83 if (files.get(item.getFieldName()) != null) { 84 values = (List) files.get(item.getFieldName()); 85 } else { 86 values = new ArrayList(); 87 } 88 89 values.add(item); 90 files.put(item.getFieldName(), values); 91 } 92 } 93 } catch (FileUploadException e) { 94 log.error(e); 95 errors.add(e.getMessage()); 96 } 97 } 98 99 public Enumeration getFileParameterNames() { 100 return Collections.enumeration(files.keySet()); 101 } 102 103 public String [] getContentType(String fieldName) { 104 List items = (List) files.get(fieldName); 105 106 if (items == null) { 107 return null; 108 } 109 110 List contentTypes = new ArrayList(items.size()); 111 for (int i = 0; i < items.size(); i++) { 112 FileItem fileItem = (FileItem) items.get(i); 113 contentTypes.add(fileItem.getContentType()); 114 } 115 116 return (String []) contentTypes.toArray(new String [contentTypes.size()]); 117 } 118 119 public File [] getFile(String fieldName) { 120 List items = (List) files.get(fieldName); 121 122 if (items == null) { 123 return null; 124 } 125 126 List fileList = new ArrayList(items.size()); 127 for (int i = 0; i < items.size(); i++) { 128 DefaultFileItem fileItem = (DefaultFileItem) items.get(i); 129 fileList.add(fileItem.getStoreLocation()); 130 } 131 132 return (File []) fileList.toArray(new File [fileList.size()]); 133 } 134 135 138 private String getCanonicalName(String filename) { 139 int forwardSlash = filename.lastIndexOf("/"); 140 int backwardSlash = filename.lastIndexOf("\\"); 141 if (forwardSlash != -1 && forwardSlash > backwardSlash) { 142 filename = filename.substring(forwardSlash + 1, filename.length()); 143 } else if (backwardSlash != -1 && backwardSlash >= forwardSlash) { 144 filename = filename.substring(backwardSlash + 1, filename.length()); 145 } 146 147 return filename; 148 } 149 150 public String [] getFileNames(String fieldName) { 151 List items = (List) files.get(fieldName); 152 153 if (items == null) { 154 return null; 155 } 156 157 List fileNames = new ArrayList(items.size()); 158 for (int i = 0; i < items.size(); i++) { 159 DefaultFileItem fileItem = (DefaultFileItem) items.get(i); 160 fileNames.add(getCanonicalName(fileItem.getName())); 161 } 162 163 return (String []) fileNames.toArray(new String [fileNames.size()]); 164 } 165 166 public String [] getFilesystemName(String fieldName) { 167 List items = (List) files.get(fieldName); 168 169 if (items == null) { 170 return null; 171 } 172 173 List fileNames = new ArrayList(items.size()); 174 for (int i = 0; i < items.size(); i++) { 175 DefaultFileItem fileItem = (DefaultFileItem) items.get(i); 176 fileNames.add(fileItem.getStoreLocation().getName()); 177 } 178 179 return (String []) fileNames.toArray(new String [fileNames.size()]); 180 } 181 182 public String getParameter(String name) { 183 List v = (List) params.get(name); 184 if (v != null && v.size() > 0) { 185 return (String ) v.get(0); 186 } 187 188 return null; 189 } 190 191 public Enumeration getParameterNames() { 192 return Collections.enumeration(params.keySet()); 193 } 194 195 public String [] getParameterValues(String name) { 196 List v = (List) params.get(name); 197 if (v != null && v.size() > 0) { 198 return (String []) v.toArray(new String [v.size()]); 199 } 200 201 return null; 202 } 203 204 public List getErrors() { 205 return errors; 206 } 207 } 208 | Popular Tags |