Created
December 20, 2017 16:45
-
-
Save gurkan0791/feb8c5bf85ce8ea4f9fd77c6ebef886c to your computer and use it in GitHub Desktop.
Spring File upload Java Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@ComponentScan("com.myspring") | |
@EnableWebMvc | |
public class Config extends WebMvcConfigurerAdapter{ | |
@Bean | |
public ViewResolver viewResolver(){ | |
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); | |
viewResolver.setViewClass(JstlView.class); | |
viewResolver.setPrefix("/WEB-INF/jsp/"); | |
viewResolver.setSuffix(".jsp"); | |
viewResolver.setOrder(0); | |
return viewResolver; | |
} | |
@Bean(name = "multipartResolver") | |
public StandardServletMultipartResolver resolver() { | |
return new StandardServletMultipartResolver(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { | |
@Override | |
protected Class<?>[] getRootConfigClasses() { | |
return new Class[] { Config.class }; | |
} | |
@Override | |
protected Class<?>[] getServletConfigClasses() { | |
return null; | |
} | |
@Override | |
protected String[] getServletMappings() { | |
return new String[] { "/" }; | |
} | |
@Override | |
protected void customizeRegistration(ServletRegistration.Dynamic registration) { | |
registration.setMultipartConfig(getMultipartConfigElement()); | |
} | |
private MultipartConfigElement getMultipartConfigElement() { | |
MultipartConfigElement multipartConfigElement = new MultipartConfigElement( LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD); | |
return multipartConfigElement; | |
} | |
// if windows then configure C:/bla/bla | |
private static final String LOCATION = "/home/gurkan"; // Temporary location where files will be stored | |
private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size. | |
// Beyond that size spring will throw exception. | |
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part. | |
private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment