Announcement Announcement Module
Collapse
No announcement yet.
How to Cleanup Multipart Files? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Cleanup Multipart Files?

    Hello.

    I am using the CommonsMultipartResolver for file uploads. However, by default it seems the temporary files are never removed. What is the best practice for removing these?

    I noticed there is a cleanupMultipart in the MultipartResolver class. Is this what I should be using? If so, is there a way to just add the cleanup to an interceptor in the afterCompletion method? Any tips on how to do this would be appreciated.

    Thanks,
    Tom

  • #2
    I figured out a solution. Here it is in case others follow in my footsteps.

    This solution uses an interceptor to ensure that any multipart files are cleaned up after the completion of the request.

    Enjoy!

    Tom

    Code:
    public class PrimaryInterceptor 
      extends HandlerInterceptorAdapter implements ApplicationContextAware {
    
    	private ApplicationContext applicationContext;
    	
    
    	@Override
    	public void afterCompletion(HttpServletRequest request, 
    	  HttpServletResponse response, Object handler, Exception ex) throws Exception {
    	  
    		// cleanup the multipart files
    		if (request instanceof MultipartHttpServletRequest) {
    			MultipartResolver resolver = (MultipartResolver) applicationContext.getBean("multipartResolver");
    			resolver.cleanupMultipart((MultipartHttpServletRequest) request);
    		}
    	}
    
    	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    		this.applicationContext = applicationContext;
    	}
    }

    Comment

    Working...
    X