1 16 17 package org.springframework.web.multipart.commons; 18 19 import java.util.List ; 20 21 import javax.servlet.ServletContext ; 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.commons.fileupload.FileItemFactory; 25 import org.apache.commons.fileupload.FileUpload; 26 import org.apache.commons.fileupload.FileUploadBase; 27 import org.apache.commons.fileupload.FileUploadException; 28 import org.apache.commons.fileupload.servlet.ServletFileUpload; 29 import org.apache.commons.fileupload.servlet.ServletRequestContext; 30 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.multipart.support.DefaultMultipartHttpServletRequest; 37 import org.springframework.web.util.WebUtils; 38 39 67 public class CommonsMultipartResolver extends CommonsFileUploadSupport 68 implements MultipartResolver, ServletContextAware { 69 70 78 public CommonsMultipartResolver() { 79 super(); 80 } 81 82 87 public CommonsMultipartResolver(ServletContext servletContext) { 88 this(); 89 setServletContext(servletContext); 90 } 91 92 98 protected FileUpload newFileUpload(FileItemFactory fileItemFactory) { 99 return new ServletFileUpload(fileItemFactory); 100 } 101 102 public void setServletContext(ServletContext servletContext) { 103 if (!isUploadTempDirSpecified()) { 104 getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext)); 105 } 106 } 107 108 109 public boolean isMultipart(HttpServletRequest request) { 110 return ServletFileUpload.isMultipartContent(new ServletRequestContext(request)); 111 } 112 113 public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { 114 String encoding = determineEncoding(request); 115 FileUpload fileUpload = prepareFileUpload(encoding); 116 try { 117 List fileItems = ((ServletFileUpload) fileUpload).parseRequest(request); 118 MultipartParsingResult parsingResult = parseFileItems(fileItems, encoding); 119 return new DefaultMultipartHttpServletRequest( 120 request, parsingResult.getMultipartFiles(), parsingResult.getMultipartParameters()); 121 } 122 catch (FileUploadBase.SizeLimitExceededException ex) { 123 throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex); 124 } 125 catch (FileUploadException ex) { 126 throw new MultipartException("Could not parse multipart servlet request", ex); 127 } 128 } 129 130 140 protected String determineEncoding(HttpServletRequest request) { 141 String encoding = request.getCharacterEncoding(); 142 if (encoding == null) { 143 encoding = getDefaultEncoding(); 144 } 145 return encoding; 146 } 147 148 public void cleanupMultipart(MultipartHttpServletRequest request) { 149 cleanupFileItems(request.getFileMap().values()); 150 } 151 152 } 153 | Popular Tags |