I'm trying to upload an image using http://www.baeldung.com/spring-file-upload I 'm calling Spring-mvc
fileUpload
in my controller and it goes in but doesn't upload the image. The steps I have followed are: Add the dependency:Commons FileUpload
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
I have set the MultipartConfigElement by adding the location and maximum size to what I already had
public class SpringWebAppInitializer implements WebApplicationInitializer {
private String TMP_FOLDER = "C:/Users/SilviaGM/Desktop/git tfg";
private int MAX_UPLOAD_SIZE = 5 * 1024 * 1024;
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class);
// Dispatcher Servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
dispatcher.setInitParameter("contextClass", appContext.getClass().getName());
servletContext.addListener(new ContextLoaderListener(appContext));
// UTF8 Charactor Filter.
FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, true, "/*");
//Nuevo para subir la imagen
ServletRegistration.Dynamic appServlet = servletContext.addServlet("mvc", new DispatcherServlet(
new GenericWebApplicationContext()));
appServlet.setLoadOnStartup(1);
MultipartConfigElement multipartConfigElement = new MultipartConfigElement(TMP_FOLDER,
MAX_UPLOAD_SIZE, MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2);
appServlet.setMultipartConfig(multipartConfigElement);
}
}
I have added the @Bean
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
// Static Resource Config
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
return multipartResolver;
}
}
And finally I have added the method to the controller:
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String submit(@RequestParam("file") MultipartFile file, ModelMap modelMap) {
modelMap.addAttribute("file", file);
System.out.println("Estoy aqui"); //Me muestra el valor por lo que es llamado el metodo.
return "fileUploadView";
}
My problem is that the image is not uploaded to the directory: "C:/Users/SilviaGM/Desktop/git tfg"; and I can't find the problem or if I'm missing something in the configuration
Hello regarding the configuration part:
The last parameter indicates when the files are going to be automatically persisted in the indicated location, therefore you have several situations between these that your file is not reaching the minimum size to be automatically persisted
If you intend to do the process manually, what you have left would be to validate that it is not empty and finally persist it in the way you prefer, be it on the hard disk, in a db, etc...
For this case I am going to do it on the hard drive since I see what you are looking for, and I will use a library that will make the process easier, but you can do it with any other method. The library that I will use is called: org.apache.commons.io
Now in your controller:
I haven't been able to test my code yet, but as you can see, you have to decide what you're going to do with the file.
Greetings.