1 16 17 package org.springframework.web.multipart.commons; 18 19 import java.io.IOException ; 20 import java.io.UnsupportedEncodingException ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.commons.fileupload.FileItem; 28 import org.apache.commons.fileupload.FileItemFactory; 29 import org.apache.commons.fileupload.FileUpload; 30 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import org.springframework.core.io.Resource; 35 import org.springframework.util.StringUtils; 36 import org.springframework.web.util.WebUtils; 37 38 58 public abstract class CommonsFileUploadSupport { 59 60 protected final Log logger = LogFactory.getLog(getClass()); 61 62 private final DiskFileItemFactory fileItemFactory; 63 64 private final FileUpload fileUpload; 65 66 private boolean uploadTempDirSpecified = false; 67 68 69 75 public CommonsFileUploadSupport() { 76 this.fileItemFactory = newFileItemFactory(); 77 this.fileUpload = newFileUpload(getFileItemFactory()); 78 } 79 80 81 86 public DiskFileItemFactory getFileItemFactory() { 87 return fileItemFactory; 88 } 89 90 95 public FileUpload getFileUpload() { 96 return fileUpload; 97 } 98 99 105 public void setMaxUploadSize(long maxUploadSize) { 106 this.fileUpload.setSizeMax(maxUploadSize); 107 } 108 109 116 public void setMaxInMemorySize(int maxInMemorySize) { 117 this.fileItemFactory.setSizeThreshold(maxInMemorySize); 118 } 119 120 134 public void setDefaultEncoding(String defaultEncoding) { 135 this.fileUpload.setHeaderEncoding(defaultEncoding); 136 } 137 138 protected String getDefaultEncoding() { 139 String encoding = getFileUpload().getHeaderEncoding(); 140 if (encoding == null) { 141 encoding = WebUtils.DEFAULT_CHARACTER_ENCODING; 142 } 143 return encoding; 144 } 145 146 151 public void setUploadTempDir(Resource uploadTempDir) throws IOException { 152 if (!uploadTempDir.exists() && !uploadTempDir.getFile().mkdirs()) { 153 throw new IllegalArgumentException ("Given uploadTempDir [" + uploadTempDir + "] could not be created"); 154 } 155 this.fileItemFactory.setRepository(uploadTempDir.getFile()); 156 this.uploadTempDirSpecified = true; 157 } 158 159 protected boolean isUploadTempDirSpecified() { 160 return uploadTempDirSpecified; 161 } 162 163 164 170 protected DiskFileItemFactory newFileItemFactory() { 171 return new DiskFileItemFactory(); 172 } 173 174 180 protected abstract FileUpload newFileUpload(FileItemFactory fileItemFactory); 181 182 183 191 protected FileUpload prepareFileUpload(String encoding) { 192 FileUpload fileUpload = getFileUpload(); 193 FileUpload actualFileUpload = fileUpload; 194 195 if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) { 198 actualFileUpload = newFileUpload(getFileItemFactory()); 199 actualFileUpload.setSizeMax(fileUpload.getSizeMax()); 200 actualFileUpload.setHeaderEncoding(encoding); 201 } 202 203 return actualFileUpload; 204 } 205 206 214 protected MultipartParsingResult parseFileItems(List fileItems, String encoding) { 215 Map multipartFiles = new HashMap (); 216 Map multipartParameters = new HashMap (); 217 218 for (Iterator it = fileItems.iterator(); it.hasNext();) { 220 FileItem fileItem = (FileItem) it.next(); 221 if (fileItem.isFormField()) { 222 String value = null; 223 if (encoding != null) { 224 try { 225 value = fileItem.getString(encoding); 226 } 227 catch (UnsupportedEncodingException ex) { 228 if (logger.isWarnEnabled()) { 229 logger.warn("Could not decode multipart item '" + fileItem.getFieldName() + 230 "' with encoding '" + encoding + "': using platform default"); 231 } 232 value = fileItem.getString(); 233 } 234 } 235 else { 236 value = fileItem.getString(); 237 } 238 String [] curParam = (String []) multipartParameters.get(fileItem.getFieldName()); 239 if (curParam == null) { 240 multipartParameters.put(fileItem.getFieldName(), new String [] { value }); 242 } 243 else { 244 String [] newParam = StringUtils.addStringToArray(curParam, value); 246 multipartParameters.put(fileItem.getFieldName(), newParam); 247 } 248 } 249 else { 250 CommonsMultipartFile file = new CommonsMultipartFile(fileItem); 252 multipartFiles.put(file.getName(), file); 253 if (logger.isDebugEnabled()) { 254 logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() + 255 " bytes with original filename [" + file.getOriginalFilename() + "], stored " + 256 file.getStorageDescription()); 257 } 258 } 259 } 260 return new MultipartParsingResult(multipartFiles, multipartParameters); 261 } 262 263 270 protected void cleanupFileItems(Collection multipartFiles) { 271 for (Iterator it = multipartFiles.iterator(); it.hasNext();) { 272 CommonsMultipartFile file = (CommonsMultipartFile) it.next(); 273 if (logger.isDebugEnabled()) { 274 logger.debug("Cleaning up multipart file [" + file.getName() + "] with original filename [" + 275 file.getOriginalFilename() + "], stored " + file.getStorageDescription()); 276 } 277 file.getFileItem().delete(); 278 } 279 } 280 281 282 286 protected static class MultipartParsingResult { 287 288 private final Map multipartFiles; 289 290 private final Map multipartParameters; 291 292 297 public MultipartParsingResult(Map multipartFiles, Map multipartParameters) { 298 this.multipartFiles = multipartFiles; 299 this.multipartParameters = multipartParameters; 300 } 301 302 305 public Map getMultipartFiles() { 306 return multipartFiles; 307 } 308 309 312 public Map getMultipartParameters() { 313 return multipartParameters; 314 } 315 } 316 317 } 318 | Popular Tags |