Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring file upload - getting Expected MultipartHttpServletRequest: is a MultipartResolver configured? error

I'm trying to incorporate multiple file upload functionality in my Angular web application using angular-file-upload. Currently, the front end functionality works, but each upload attempt throws a

java.lang.IllegalStateException,java.io.IOException]: 
    java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: 
        is a MultipartResolver configured?

exception.

The Upload Controller is defined as

@Controller
@PropertySource("classpath:application.properties")
public class FileUploadController {

    @Resource
    private Environment env;

    @RequestMapping(value = "/fileupload", method = RequestMethod.POST)
    @ResponseBody
    public List<String> fileUpload(@RequestParam("file") MultipartFile[] uploadFiles) throws IllegalStateException, IOException {
        //file processing logic
    }
 }

In my AppConfig.java class, I declare the bean

@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setDefaultEncoding("utf-8");
    commonsMultipartResolver.setMaxUploadSize(50000000);
    return commonsMultipartResolver;
}

and start the web application with

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));

    ctx.setServletContext(servletContext);
    ctx.refresh();

    Dynamic servlet = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
    //servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
}

I do not think it is due to the lack of a <form> element in my Angular view, because I can see that Content-Tyle is multipart/form-data and that the Request Payload is set appropriately.

Remote Address:192.168.33.10:80
Request URL:http://dev.jason.com/rest/fileupload
Request Method:POST
Status Code:500 Internal Server Error
Request Headers
  Accept:*/*
  Accept-Encoding:gzip,deflate
  Accept-Language:en-US,en;q=0.8
  Cache-Control:no-cache
  Connection:keep-alive
  Content-Length:415235
  Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryBHlsldPQysTVpvwZ
  Host:dev.jason.com
  Origin:http://dev.jason.com
  Pragma:no-cache
  Referer:http://dev.jason.com/
  User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36
Request Payload
   ------WebKitFormBoundaryBHlsldPQysTVpvwZ
   Content-Disposition: form-data; name="file"; filename="IMG_5072.jpg"
   Content-Type: image/jpeg

   ------WebKitFormBoundaryBHlsldPQysTVpvwZ--

Note that this issue still occurs when including

@Bean
public MultipartConfigElement multipartConfigElement(){
    MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
    multipartConfigFactory.setMaxFileSize("10MB");
    multipartConfigFactory.setMaxRequestSize("50MB");
    return multipartConfigFactory.createMultipartConfig();
}

in AppConfig.java and uncommenting the command

servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));

in my servlet initializer class.

Any help is appreciated!

like image 334
Jason Avatar asked Nov 20 '14 21:11

Jason


2 Answers

Spring's MVC stack requires that the MultipartResolver bean be called multipartResolver. Change your @Bean method to

@Bean
public CommonsMultipartResolver multipartResolver(){

or

@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){
like image 157
Sotirios Delimanolis Avatar answered Nov 10 '22 22:11

Sotirios Delimanolis


Just add this to servler-context.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="maxUploadSize" value="268435456"/>
</bean>

Nothing else needed.

like image 10
Patrice Avatar answered Nov 11 '22 00:11

Patrice