Spring Migration: Set default Encoding on StandardServletMultipartResolver

103 Views Asked by At

I'm migrating a Spring 5 application to Spring 6. In the old code I was using the CommonsMultipartResolver as a MultipartResolver-Bean. Since Spring 6 the CommonsMultipartResolver is gone and I have to use the StandardServletMultipartResolver, but this class does not have a setDefaultEncoding method, which was previously used. What's the new recommended way to achieve the same behavior?

I googled but i could only find migration guides for the file-size related methods of the old CommonsMultipartResolver.

1

There are 1 best solutions below

0
Seenu On

Same thing happened for me also I have Removed commons-fileupload & commons-io dependency

1 Define the StandardServletMultipartResolver bean in your Spring configuration file

@Bean 
public StandardServletMultipartResolver multipartResolver() 
{ 
    return new StandardServletMultipartResolver(); 
}

For parsing multipart request,We need to set a MultipartConfigElement in our DispatcherServletregistration :

public class Initializer implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));
        ctx.setServletContext(servletContext);
        ctx.getEnvironment().setActiveProfiles(servletContext.getInitParameter(SPRING_ACTIVE_PROFILE));
        ServletRegistration.Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
        servlet.setMultipartConfig(new MultipartConfigElement("", 50000000, 50000000, 0));
    }
}

You can give your MAXSIZE for File Upload