Spring Upload File

Commons FileUpload multipart request process

  • XML-based
  • Java-based

XML-based

web.xml

<servlet>
  <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

mvc-config.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <property name="uploadTempDir" value="/tmp"></property>
   <property name="maxUploadSize" value="1048576"></property>
   <property name="maxInMemorySize" value="524288"></property>
</bean>

 

Java-based

WebInitializer

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

  @Override
  protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { ServiceConfiguration.class };
  }

  @Override
  protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebMvcConfiguration.class };
  }

  @Override
  protected String[] getServletMappings() {
    return new String[] {"/"};
  }
	
}

WebMvcConfiguration.java

@EnableWebMvc
@Configuration
@ComponentScan(basePackageClasses = Application.class)
public class WebMvcConfiguration {

  @Bean
  public InternalResourceViewResolver getInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/view/");
    resolver.setSuffix(".jsp");
    return resolver;
  } 

  @Bean(name = "multipartResolver") 
  public CommonsMultipartResolver multipartResolver() throws IOException { 
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    resolver.setUploadTempDir(new FileSystemResource("/tmp"));
    resolver.setMaxUploadSize(1048576);
    resolver.setMaxInMemorySize(524288);
    return resolver;
   }
	
}

 

 

Servlet 3.1 multipart request process

  • XML-based
  • Java-based

XML-based

web.xml

<servlet>
  <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
		
   <multipart-config>
        <location>/tmp</location>
        <max-file-size>1048576</max-file-size>
        <max-request-size>2097152</max-request-size>
        <file-size-threshold>524288</file-size-threshold>
   </multipart-config>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

mvc-config.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver" />

Java-Based

Define MultipartConfigElement in programmatic Servlet registration.

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

  @Override
  protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { ServiceConfiguration.class };
  }

  @Override
  protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebMvcConfiguration.class };
  }

  @Override
  protected String[] getServletMappings() {
    return new String[] {"/"};
  }

  @Override
  protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        
    MultipartConfigElement multipartConfigElement =
      new MultipartConfigElement(null, 5000000, 5000000, 0);
    registration.setMultipartConfig(multipartConfigElement);
    registration.setLoadOnStartup(1);
    registration.addMapping("/");
  }
	
}

 

 

Reference:

http://docs.spring.io/spring/docs/4.0.4.RELEASE/spring-framework-reference/htmlsingle/#mvc-multipart

Spring Upload File

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.