From ecc662501780e7337bc0ca0cda627bb95de3e104 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:49:55 +0100 Subject: [PATCH 1/7] Add form tag example --- .../org/baeldung/spring/ClientWebConfig.java | 8 +-- .../baeldung/spring/ClientWebConfigJava.java | 53 +++++++++++++------ .../main/webapp/WEB-INF/view/personForm.jsp | 6 +-- .../main/webapp/WEB-INF/view/personView.jsp | 15 ------ .../src/main/webapp/WEB-INF/web.xml | 2 +- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java index c4d819caa5..9705d51606 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java @@ -8,10 +8,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @Configuration public class ClientWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { - super(); - } + public ClientWebConfig() { + super(); + } - // API + // API } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java index d2b57da818..c82201880f 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java @@ -1,6 +1,12 @@ package org.baeldung.spring; +import java.util.Locale; +import java.util.ResourceBundle; + +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.support.MessageSourceResourceBundle; +import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -11,27 +17,40 @@ import org.springframework.web.servlet.view.JstlView; //@Configuration public class ClientWebConfigJava extends WebMvcConfigurerAdapter { - public ClientWebConfigJava() { - super(); - } + public ClientWebConfigJava() { + super(); + } - // API + @Bean + public MessageSource messageSource() { - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); + final ResourceBundleMessageSource ms = new ResourceBundleMessageSource(); + ms.setBasenames("messages"); + return ms; + } - registry.addViewController("/sample.html"); - } + @Bean + public ResourceBundle getBeanResourceBundle() { - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + final Locale locale = Locale.getDefault(); + return new MessageSourceResourceBundle(messageSource(), locale); + } - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); - return bean; - } + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } } \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 79bda7c17d..63d18a987c 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -24,7 +24,7 @@

Welcome, Enter The Person Details

- + @@ -104,10 +104,6 @@ Notes - - Select a file to upload - - diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp index 8893314d20..bd14474dfe 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp @@ -61,20 +61,5 @@ ${person.notes} - -

Submitted File

- - - - - - - - - - - - -
OriginalFileName :${person.file.originalFilename}
Type :${person.file.contentType}
\ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..4e38b2fae6 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet 1 - /tmp + C:/Users/ivan/Desktop/tmp From 934c0dda3740ae8142fadef664efcc26f7537d0f Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:51:16 +0100 Subject: [PATCH 2/7] Add form tag example --- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 4e38b2fae6..a39c4fdd3a 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet 1 - C:/Users/ivan/Desktop/tmp + /tmp From 925f174c54672c5436f3d83d1ad43e77a9bd49ae Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 4 Mar 2016 14:53:39 +0100 Subject: [PATCH 3/7] Add form tag example --- spring-mvc-xml/src/main/resources/webMvcConfig.xml | 7 ------- spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp | 2 +- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 3 --- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index f3297fdbf6..f127cfd637 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -25,11 +25,4 @@ - - - - - \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 63d18a987c..2836c11668 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -22,7 +22,7 @@ -

Welcome, Enter The Person Details

+

Welcome, Enter the Person Details

diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..5275efdf24 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -28,9 +28,6 @@ mvc org.springframework.web.servlet.DispatcherServlet 1 - - /tmp - mvc From 8d398b28529fd16316f38496617894fde2bf2d05 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 23 Mar 2016 13:12:12 +0100 Subject: [PATCH 4/7] Add file upload example --- spring-mvc-java/.classpath | 74 ++++++------ spring-mvc-java/pom.xml | 7 ++ .../spring/web/config/ClientWebConfig.java | 112 +++++++++--------- .../web/config/MainWebAppInitializer.java | 50 ++++---- .../baeldung/spring/web/config/WebConfig.java | 15 +++ .../web/controller/FileUploadController.java | 80 +++++++++++++ .../webapp/WEB-INF/view/fileUploadForm.jsp | 47 ++++++++ .../webapp/WEB-INF/view/fileUploadView.jsp | 20 ++++ 8 files changed, 293 insertions(+), 112 deletions(-) create mode 100644 spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp create mode 100644 spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp diff --git a/spring-mvc-java/.classpath b/spring-mvc-java/.classpath index 6b533711d3..a642d37ceb 100644 --- a/spring-mvc-java/.classpath +++ b/spring-mvc-java/.classpath @@ -1,37 +1,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index d9a578cb8c..4358e7939f 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -57,6 +57,13 @@ slf4j-log4j12 ${org.slf4j.version} + + + commons-fileupload + commons-fileupload + 1.3.1 + + junit diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index db57b4716b..3f451f4259 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -25,69 +25,71 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver; @Configuration public class ClientWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { - super(); - } + public ClientWebConfig() { + super(); + } - // API + // API - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); - registry.addViewController("/sample.html"); - } + registry.addViewController("/sample.html"); + registry.addViewController("/fileUpload.html"); + registry.addViewController("/fileUploadForm.html"); + } - @Bean - public ViewResolver thymeleafViewResolver() { - final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); - viewResolver.setTemplateEngine(templateEngine()); - viewResolver.setOrder(1); - return viewResolver; - } + @Bean + public ViewResolver thymeleafViewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + return viewResolver; + } - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - bean.setOrder(0); - return bean; - } + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } - @Bean - @Description("Thymeleaf template resolver serving HTML 5") - public ServletContextTemplateResolver templateResolver() { - final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); - templateResolver.setPrefix("/WEB-INF/templates/"); - templateResolver.setSuffix(".html"); - templateResolver.setTemplateMode("HTML5"); - return templateResolver; - } + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } - @Bean - @Description("Thymeleaf template engine with Spring integration") - public SpringTemplateEngine templateEngine() { - final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); - templateEngine.setTemplateResolver(templateResolver()); - final Set dialects = new HashSet<>(); - dialects.add(new CustomDialect()); - templateEngine.setAdditionalDialects(dialects); - return templateEngine; - } + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + final Set dialects = new HashSet<>(); + dialects.add(new CustomDialect()); + templateEngine.setAdditionalDialects(dialects); + return templateEngine; + } - @Bean - @Description("Spring message resolver") - public MessageSource messageSource() { - final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename("messages"); - return messageSource; - } + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } - @Override - public void addResourceHandlers(final ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); - } + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } } \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java index 87502e2088..9dd9ae98b0 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java @@ -14,28 +14,38 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { - /** - * Register and configure all Servlet container components necessary to power the web application. - */ - @Override - public void onStartup(final ServletContext sc) throws ServletException { - System.out.println("MainWebAppInitializer.onStartup()"); + private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5 + // MB + private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB - // Create the 'root' Spring application context - final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.baeldung.spring.web.config"); - // root.getEnvironment().setDefaultProfiles("embedded"); + /** + * Register and configure all Servlet container components necessary to power the web application. + */ + @Override + public void onStartup(final ServletContext sc) throws ServletException { - // Manages the lifecycle of the root application context - sc.addListener(new ContextLoaderListener(root)); + // Create the 'root' Spring application context + final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.scan("org.baeldung.spring.web.config"); + // root.getEnvironment().setDefaultProfiles("embedded"); - // Handles requests into the application - final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); - appServlet.setLoadOnStartup(1); - final Set mappingConflicts = appServlet.addMapping("/"); - if (!mappingConflicts.isEmpty()) { - throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); - } - } + // Manages the lifecycle of the root application context + sc.addListener(new ContextLoaderListener(root)); + + // Handles requests into the application + final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); + appServlet.setLoadOnStartup(1); + + // final MultipartConfigElement multipartConfigElement = new + // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, + // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); + // + // appServlet.setMultipartConfig(multipartConfigElement); + + final Set mappingConflicts = appServlet.addMapping("/"); + if (!mappingConflicts.isEmpty()) { + throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); + } + } } diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index 58438d2976..b7d87dffbc 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -22,6 +22,21 @@ public class WebConfig extends WebMvcConfigurerAdapter { super(); } + // @Bean + // public StandardServletMultipartResolver multipartResolver() { + // return new StandardServletMultipartResolver(); + // } + + // @Bean(name = "multipartResolver") + // public CommonsMultipartResolver multipartResolver() { + // + // final CommonsMultipartResolver multipartResolver = new + // CommonsMultipartResolver(); + // multipartResolver.setMaxUploadSize(100000); + // + // return multipartResolver; + // } + @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java new file mode 100644 index 0000000000..5c9d3cbef3 --- /dev/null +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -0,0 +1,80 @@ +package org.baeldung.web.controller; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.io.FileUtils; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +@Controller +public class FileUploadController { + + @RequestMapping(value = "/addFile1", method = RequestMethod.POST) + public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { + + modelMap.addAttribute("fileName", file.getOriginalFilename()); + modelMap.addAttribute("fileType", file.getContentType()); + + return "fileUploadView"; + } + + @RequestMapping(value = "/addFile2", method = RequestMethod.POST) + public String submit(final HttpServletRequest request, final HttpServletResponse response, + final ModelMap modelMap) { + + final String TEMP_PATH = "C:\\Users\\ivan\\Desktop\\tmp\\"; + + try { + + final DiskFileItemFactory factory = new DiskFileItemFactory(); + + // Configure a repository (to ensure a secure temp location is used) + final File repository = new File(TEMP_PATH); + factory.setRepository(repository); + + // Create a new file upload handler + final ServletFileUpload upload = new ServletFileUpload(factory); + + // Parse the request + final List items = upload.parseRequest(request); + + final Iterator iter = items.iterator(); + + while (iter.hasNext()) { + + final FileItem item = iter.next(); + + if (!item.isFormField()) { + + final File targetFile = new File(TEMP_PATH + item.getName()); + FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile); + + modelMap.addAttribute("fileName", item.getName()); + modelMap.addAttribute("fileType", item.getContentType()); + } + } + + } catch (final FileUploadException e) { + e.printStackTrace(); + + } catch (final IOException e) { + e.printStackTrace(); + } + + return "fileUploadView"; + } +} diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp new file mode 100644 index 0000000000..05a7e25a02 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -0,0 +1,47 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + File Upload Example + + + + +

Enter The File to Upload (MultipartFile handling)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ +
+ +

Enter The File to Upload (HttpServletRequest handling)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp new file mode 100644 index 0000000000..779c26a597 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -0,0 +1,20 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC File Upload + + +

Submitted File

+ + + + + + + + + +
OriginalFileName :${fileName}
Type :${fileType}
+ + \ No newline at end of file From 09a1ebfa07b9d697fc4973aa20d87700bc8aa370 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 23 Mar 2016 13:16:10 +0100 Subject: [PATCH 5/7] Add file upload example --- .../java/org/baeldung/web/controller/FileUploadController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java index 5c9d3cbef3..a25a620c8b 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -36,7 +36,7 @@ public class FileUploadController { public String submit(final HttpServletRequest request, final HttpServletResponse response, final ModelMap modelMap) { - final String TEMP_PATH = "C:\\Users\\ivan\\Desktop\\tmp\\"; + final String TEMP_PATH = "/tmp/"; try { From b7d0b2947a0bffd2c89639425dd5a7b6bd91d4cb Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 30 Mar 2016 15:06:37 +0200 Subject: [PATCH 6/7] Add file upload example --- .../spring/web/config/ClientWebConfig.java | 2 - .../web/config/MainWebAppInitializer.java | 3 +- .../baeldung/spring/web/config/WebConfig.java | 18 ++--- .../web/controller/FileUploadController.java | 70 +++---------------- .../webapp/WEB-INF/view/fileUploadForm.jsp | 22 ++++-- .../webapp/WEB-INF/view/fileUploadView.jsp | 22 +++++- 6 files changed, 55 insertions(+), 82 deletions(-) diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java index 3f451f4259..6084943ddd 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java @@ -36,8 +36,6 @@ public class ClientWebConfig extends WebMvcConfigurerAdapter { super.addViewControllers(registry); registry.addViewController("/sample.html"); - registry.addViewController("/fileUpload.html"); - registry.addViewController("/fileUploadForm.html"); } @Bean diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java index 9dd9ae98b0..ad37bbec5e 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java @@ -14,8 +14,7 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { - private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5 - // MB + private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB /** diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index b7d87dffbc..cd9ae582d8 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; @@ -27,15 +28,14 @@ public class WebConfig extends WebMvcConfigurerAdapter { // return new StandardServletMultipartResolver(); // } - // @Bean(name = "multipartResolver") - // public CommonsMultipartResolver multipartResolver() { - // - // final CommonsMultipartResolver multipartResolver = new - // CommonsMultipartResolver(); - // multipartResolver.setMaxUploadSize(100000); - // - // return multipartResolver; - // } + @Bean(name = "multipartResolver") + public CommonsMultipartResolver multipartResolver() { + + final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); + multipartResolver.setMaxUploadSize(100000); + + return multipartResolver; + } @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java index a25a620c8b..6f557adf0f 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/org/baeldung/web/controller/FileUploadController.java @@ -1,18 +1,5 @@ package org.baeldung.web.controller; -import java.io.File; -import java.io.IOException; -import java.util.Iterator; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.fileupload.FileUploadException; -import org.apache.commons.fileupload.disk.DiskFileItemFactory; -import org.apache.commons.fileupload.servlet.ServletFileUpload; -import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,58 +10,23 @@ import org.springframework.web.multipart.MultipartFile; @Controller public class FileUploadController { - @RequestMapping(value = "/addFile1", method = RequestMethod.POST) + @RequestMapping(value = "/fileUpload", method = RequestMethod.GET) + public String displayForm() { + + return "fileUploadForm"; + } + + @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { - modelMap.addAttribute("fileName", file.getOriginalFilename()); - modelMap.addAttribute("fileType", file.getContentType()); - + modelMap.addAttribute("file", file); return "fileUploadView"; } - @RequestMapping(value = "/addFile2", method = RequestMethod.POST) - public String submit(final HttpServletRequest request, final HttpServletResponse response, - final ModelMap modelMap) { - - final String TEMP_PATH = "/tmp/"; - - try { - - final DiskFileItemFactory factory = new DiskFileItemFactory(); - - // Configure a repository (to ensure a secure temp location is used) - final File repository = new File(TEMP_PATH); - factory.setRepository(repository); - - // Create a new file upload handler - final ServletFileUpload upload = new ServletFileUpload(factory); - - // Parse the request - final List items = upload.parseRequest(request); - - final Iterator iter = items.iterator(); - - while (iter.hasNext()) { - - final FileItem item = iter.next(); - - if (!item.isFormField()) { - - final File targetFile = new File(TEMP_PATH + item.getName()); - FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile); - - modelMap.addAttribute("fileName", item.getName()); - modelMap.addAttribute("fileType", item.getContentType()); - } - } - - } catch (final FileUploadException e) { - e.printStackTrace(); - - } catch (final IOException e) { - e.printStackTrace(); - } + @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST) + public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) { + modelMap.addAttribute("files", files); return "fileUploadView"; } } diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 05a7e25a02..371acdc0ed 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -8,13 +8,13 @@ -

Enter The File to Upload (MultipartFile handling)

+

Enter The File to Upload

- + - + @@ -24,16 +24,24 @@ -
+
-

Enter The File to Upload (HttpServletRequest handling)

+

Enter The Files to Upload (Multiple files)

- +
Select a file to uploadSelect a file to upload (Single file)
- + + + + + + + + + diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp index 779c26a597..d6f748c6af 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -5,16 +5,32 @@ Spring MVC File Upload -

Submitted File

+ +

Submitted File (Single)

Select a file to upload
Select a file to upload
Select a file to upload
- + - +
OriginalFileName :${fileName}${file.originalFilename}
Type :${fileType}${file.contentType}
+
+ +

Submitted Files (Multiple)

+ + + + + + + + + + + +
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
\ No newline at end of file From 3061fc7c0f9b17bf517259154900e9a70a0c512b Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 4 Apr 2016 14:03:44 +0200 Subject: [PATCH 7/7] Add file upload example --- .../src/main/webapp/WEB-INF/view/fileUploadForm.jsp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 371acdc0ed..1414b824ff 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -8,13 +8,13 @@ -

Enter The File to Upload

+

Enter The File to Upload (Single file)

- +
Select a file to upload (Single file)Select a file to upload