title: Spring Rest Multipart file upload with AngularJS (Java Config)?
body: <p>I have used spring MVC with java config and I have defined some rest-services. I used Spring as server backend and AngularJS as WebFrontend.</p>
  
  <p>I want upload from my AngularJS site one or two CSV files to my rest service. How must i configure spring with java config so that it works? I used Tomcat and a Servlet 3 Container.</p>
  
  <p>My upload rest service looks like this:</p>
  
  <pre><code>@Controller
  @RequestMapping("Upload")
  @MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB
  maxFileSize=1024*1024*50,          // 50 MB
  maxRequestSize=1024*1024*100)      // 100 MB
  public class UploadController {
  
      @RequestMapping(value="/upload", method=RequestMethod.GET)
      public @ResponseBody String provideUploadInfo() {
          return "You can upload a file by posting to this same URL.";
      }
  
      @RequestMapping(value="/upload", method=RequestMethod.POST)
      public @ResponseBody String handleFileUpload(@RequestParam("name") String name, 
              @RequestParam("file") MultipartFile file){
          if (!file.isEmpty()) {
              try {
                  byte[] bytes = file.getBytes();
                  BufferedOutputStream stream = 
                          new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                  stream.write(bytes);
                  stream.close();
                  return "You successfully uploaded " + name + " into " + name + "-uploaded !";
              } catch (Exception e) {
                  return "You failed to upload " + name + " =&gt; " + e.getMessage();
              }
          } else {
              return "You failed to upload " + name + " because the file was empty.";
          }
      }
  
  }
  </code></pre>
  
  <p>Do i need a "StandardServletMultipartResolver" ? or anything else to get the upload to work ? and if so, how can i enable the multi-part upload with java config ? my config at the moment looks like this:</p>
  
  <pre><code>public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class&lt;?&gt;[]{ PersistenceContext.class,AppConfig.class,SecurityConfig.class }; 
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class&lt;?&gt;[] { WebConfig.class };
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/Spring/*" }; 
      }
  
      @Override
      protected Filter[] getServletFilters() {
  
          CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
          characterEncodingFilter.setEncoding("UTF-8");
  
          CorsFilter cf=new CorsFilter();
              MultipartFilter mpf=new MultipartFilter(); //MultipartFilter support
          return new Filter[] {characterEncodingFilter,cf,mpf};
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>and WebConfig:</p>
  
  <pre><code>@Configuration
      @EnableWebMvc
      @ComponentScan(basePackages = { "controller" })
      public class WebConfig extends WebMvcConfigurerAdapter {
  
         @Bean
  public MultipartResolver multipartResolver(){
  
          return new StandardServletMultipartResolver();
      }
  </code></pre>
  
  <p>}</p>
  
  <p>and ApplicationConfig</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Bean
      public Test getTest(){
  
  
          return new Test();
      }
  }
  </code></pre>
  
  <p>The Example with the upload is from this site <a href="http://spring.io/guides/gs/uploading-files/">http://spring.io/guides/gs/uploading-files/</a> but they used a MultiPartConfigFactory and this is from spring boot; can i use this to ? i did not use spring boot at the moment.. So What is the easiest way to get the multi-part upload working with my configuration!?</p>
  
  <p>EDIT2: I have added two things to my configuration Above the new getServletFilter looks now like this, i have added a MultipartFilter():</p>
  
  <pre><code>@Override
      protected Filter[] getServletFilters() {
  
          CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
          characterEncodingFilter.setEncoding("UTF-8");
  
  
          CorsFilter cf=new CorsFilter();
  
          MultipartFilter mpf=new MultipartFilter(); //MultipartFilter support
  
  
          return new Filter[] {characterEncodingFilter,cf,mpf};
  
      }
  </code></pre>
  
  <p>and in my WebConfig i have added a StandardServletMultipartResolver:</p>
  
  <pre><code>@Bean
  public MultipartResolver multipartResolver(){
  
          return new StandardServletMultipartResolver();
      }
  </code></pre>
  
  <p>Now i want to test it with a simple html site like this:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form method="POST" enctype="multipart/form-data"
          action="http://localhost:8081/project/Spring/Upload/upload"&gt;
          File to upload: &lt;input type="file" name="file"&gt;&lt;br /&gt; Name: &lt;input
              type="text" name="name"&gt;&lt;br /&gt; &lt;br /&gt; &lt;input type="submit"
              value="Upload"&gt; Press here to upload the file!
      &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>but it says "HTTP Status 400 - Required String parameter 'name' is not present". why is name not present !?</p>