Search code examples
javaspringspring-mvcfile-uploadresttemplate

Spring rest MultiPart file upload with Java configuration without Spring boot


I am trying to implement a rest web service that uses MultipartFile to upload a file using Spring, with java configuration. I do not use Spring Boot and I have commons-fileupload library in my classpath.

I read Spring documentation that says:

you need to mark the DispatcherServlet with a "multipart-config" section in web.xml, or with a javax.servlet.MultipartConfigElement in programmatic Servlet registration, or in case of a custom Servlet class possibly with a javax.servlet.annotation.MultipartConfig annotation on your Servlet class ... Once Servlet 3.0 multipart parsing has been enabled in one of the above mentioned ways you can add the StandardServletMultipartResolver to your Spring configuration

Hence I added this bean to my AppConfig class:

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

and annotated the class with MultipartConfig:

@EnableWebMvc
@MultipartConfig(maxFileSize = 5120)
public class AppConfig extends WebMvcConfigurerAdapter{
 ...
}

but I get this exception when I call the service:

Caused by: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart request
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:111)
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:85)
    at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76)
    at org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:112)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
    at [internal classes]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
    ... 1 more
Caused by: java.lang.UnsupportedOperationException: SRVE8020E: Servlet does not accept multipart request
    at com.ibm.ws.webcontainer.srt.SRTServletRequest.prepareMultipart(SRTServletRequest.java:3657)
    at [internal classes]
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:92)

If I use CommonsMultipartResolver instead of StandardServletMultipartResolver I get the same error.

This is how I initialize my application:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);
        context.setServletContext(servletContext);

        servletContext.addListener(new ContextLoaderListener(context));

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");


        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);

        EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
        FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("CharacterEncodingFilter", characterEncodingFilter);
        characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

    }
}

I also tried add a MultipartFilter but with no luck.

MultipartFilter multipartFilter = new MultipartFilter();
FilterRegistration.Dynamic multipart = servletContext.addFilter("multipartFilter", multipartFilter);
    multipart.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");

Is this necessary? What am I doing wrong? I think I read the whole internet searching for a solution but they all use spring boot with MultipartConfigElement and MultipartConfigFactory. Maybe the problem is the way I consume the service?

This is my controller method:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data" )
public Long uploadAttachment(@RequestParam("cn") String callerName, @RequestParam("cs") String callerService, @RequestParam("file")  MultipartFile file)

and this is how i consume it:

File file = new File("C:\\Users\\cte0289\\Documents\\Email\\document.docx");
RestTemplate rest = new RestTemplate();
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

map.add("cn", callerName);
map.add("cs", callerService);
map.add("file", file);            
Long response = rest.postForObject(url + "/upload", map, Long.class);

Please help I don't know what else to do.


Solution

  • The correct way to configure Spring project to handle file upload with java configuration is this: If you want to configure it with Commons FileUpload library you have only to include this bean in your Configuration class and add the jar in your classpath

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(5242880); // set the size limit
        return resolver;
    }
    

    if you want to configure the project with Servlet 3.0, as @AlieneilA said you have to set the MultipartConfig element in dispatcher servlet:

    dispatcher.setMultipartConfig(new MultipartConfigElement("C:/tmp", 1024*1024*5, 1024*1024*5*5, 1024*1024));
    

    and include this bean in configuration class (AppConfig in my case):

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

    I was wrong in the way i inserted the file into the LinkedMultiValueMap. I had to use a FileSystemResource:

    File file = new File("C:\\document.doc");
    RestTemplate rest = new RestTemplate();
    LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
    
    map.add("param1", param1);
    map.add("param2", param2);
    map.add("file", new FileSystemResource(file));            
    Long response = rest.postForObject(url, map, Long.class);
    

    or a MockMultipartFile as suggested by this answer: https://stackoverflow.com/a/38270420/6503002

    In this case include spring mock as dependency:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-mock</artifactId>
        <version>2.0.8</version>
    </dependency>