1 16 17 package org.springframework.web.multipart.cos; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Enumeration ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 25 26 import com.oreilly.servlet.MultipartRequest; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.springframework.core.io.Resource; 31 import org.springframework.web.context.ServletContextAware; 32 import org.springframework.web.multipart.MaxUploadSizeExceededException; 33 import org.springframework.web.multipart.MultipartException; 34 import org.springframework.web.multipart.MultipartHttpServletRequest; 35 import org.springframework.web.multipart.MultipartResolver; 36 import org.springframework.web.util.WebUtils; 37 38 52 public class CosMultipartResolver implements MultipartResolver, ServletContextAware { 53 54 57 public static final String MULTIPART_CONTENT_TYPE = "multipart/form-data"; 58 59 60 61 protected final Log logger = LogFactory.getLog(getClass()); 62 63 private int maxUploadSize = Integer.MAX_VALUE; 64 65 private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; 66 67 private File uploadTempDir; 68 69 70 78 public CosMultipartResolver() { 79 } 80 81 87 public CosMultipartResolver(ServletContext servletContext) { 88 this.uploadTempDir = WebUtils.getTempDir(servletContext); 89 } 90 91 96 public void setMaxUploadSize(int maxUploadSize) { 97 this.maxUploadSize = maxUploadSize; 98 } 99 100 103 protected int getMaxUploadSize() { 104 return maxUploadSize; 105 } 106 107 121 public void setDefaultEncoding(String defaultEncoding) { 122 this.defaultEncoding = defaultEncoding; 123 } 124 125 128 protected String getDefaultEncoding() { 129 return defaultEncoding; 130 } 131 132 137 public void setUploadTempDir(Resource uploadTempDir) throws IOException { 138 if (!uploadTempDir.exists() && !uploadTempDir.getFile().mkdirs()) { 139 throw new IllegalArgumentException ("Given uploadTempDir [" + uploadTempDir + "] could not be created"); 140 } 141 this.uploadTempDir = uploadTempDir.getFile(); 142 } 143 144 147 protected File getUploadTempDir() { 148 return uploadTempDir; 149 } 150 151 public void setServletContext(ServletContext servletContext) { 152 if (this.uploadTempDir == null) { 153 this.uploadTempDir = WebUtils.getTempDir(servletContext); 154 } 155 } 156 157 158 public boolean isMultipart(HttpServletRequest request) { 159 return request.getContentType() != null && request.getContentType().startsWith(MULTIPART_CONTENT_TYPE); 160 } 161 162 public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { 163 try { 164 MultipartRequest multipartRequest = newMultipartRequest(request); 165 if (logger.isDebugEnabled()) { 166 Enumeration fileNames = multipartRequest.getFileNames(); 167 while (fileNames.hasMoreElements()) { 168 String fileName = (String ) fileNames.nextElement(); 169 File file = multipartRequest.getFile(fileName); 170 logger.debug("Found multipart file '" + fileName + "' of size " + (file != null ? file.length() : 0) + 171 " bytes with original filename [" + multipartRequest.getOriginalFileName(fileName) + 172 "], " + (file != null ? "stored at [" + file.getAbsolutePath() + "]" : "empty")); 173 } 174 } 175 return new CosMultipartHttpServletRequest(request, multipartRequest); 176 } 177 catch (IOException ex) { 178 if (ex.getMessage().indexOf("exceeds limit") != -1) { 181 throw new MaxUploadSizeExceededException(this.maxUploadSize, ex); 182 } 183 else { 184 throw new MultipartException("Could not parse multipart request", ex); 185 } 186 } 187 } 188 189 196 protected MultipartRequest newMultipartRequest(HttpServletRequest request) throws IOException { 197 String tempPath = this.uploadTempDir.getAbsolutePath(); 198 String enc = determineEncoding(request); 199 return new MultipartRequest(request, tempPath, this.maxUploadSize, enc); 200 } 201 202 212 protected String determineEncoding(HttpServletRequest request) { 213 String enc = request.getCharacterEncoding(); 214 if (enc == null) { 215 enc = this.defaultEncoding; 216 } 217 return enc; 218 } 219 220 public void cleanupMultipart(MultipartHttpServletRequest request) { 221 MultipartRequest multipartRequest = ((CosMultipartHttpServletRequest) request).getMultipartRequest(); 222 Enumeration fileNames = multipartRequest.getFileNames(); 223 while (fileNames.hasMoreElements()) { 224 String fileName = (String ) fileNames.nextElement(); 225 File file = multipartRequest.getFile(fileName); 226 if (file != null) { 227 if (file.exists()) { 228 if (file.delete()) { 229 if (logger.isDebugEnabled()) { 230 logger.debug("Cleaned up multipart file '" + fileName + "' with original filename [" + 231 multipartRequest.getOriginalFileName(fileName) + "], stored at [" + 232 file.getAbsolutePath() + "]"); 233 } 234 } 235 else { 236 logger.warn("Could not delete multipart file '" + fileName + "' with original filename [" + 237 multipartRequest.getOriginalFileName(fileName) + "], stored at [" + 238 file.getAbsolutePath() + "]"); 239 } 240 } 241 else { 242 if (logger.isDebugEnabled()) { 243 logger.debug("Multipart file '" + fileName + "' with original filename [" + 244 multipartRequest.getOriginalFileName(fileName) + 245 "] has already been moved - no cleanup necessary"); 246 } 247 } 248 } 249 } 250 } 251 252 } 253 | Popular Tags |