Download Excel file via AJAX MVC. Ask Question 77. I have a large(ish) form in MVC. I need to be able to generate an excel file containing data from a subset of that form. Download Excel using AJAX in MVC not Working. Downloading files with Ajax.BeginForm. This post shows uploading files into Database using Spring MVC 4, Hibernate & MySQL Database. Spring MVC File upload database example, download and delete from database using Hibernate+MySQL. Let’s get going.
Spring MVC provides easy way to upload files, it may be image or other files. Let's see a simple example to upload file using Spring MVC.
Required Jar files
To run this example, you need to load:
- Spring Core jar files
- Spring Web jar files
- commons-fileupload.jar and commons-io.jar file
1) Download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.
2) Download commons-io.jar
3) Download commons-fileupload.jar
Spring MVC File Upload Steps (Extra than MVC)
1) Add commons-io and fileupload.jar files
2) Add entry of CommonsMultipartResolver in spring-servlet.xml
3) Create form to submit file. Method name must be 'post' and enctype 'multiple/form-data'.
4) Use CommonsMultipartFile class in Controller.
5) Display image in JSP.
Spring MVC File Upload Example
Create images directoryCreate 'images' directory in your project because we are writing the code to save all the files inside '/images' directory.
index.jspEmp.javaweb.xmlspring-servlet.xmlHere, you need to create a bean for CommonsMultipartResolver.
uploadform.jspHere form must be method='post' and enctype='multipart/form-data'.
Spring Mvc Flow
Spring >
Import/Read excel file 2003 or 2007 with Spring MVC
posted Aug 8, 2016, 3:21 AM by Movies Clip How To Insert Data Using Spring Mvc Netbeans
source: http://lvtutorial.com/spring-mvc/read-excel-file-2003-or-2007-with-spring-mvc.html To continue with previous download excel file part, we develop import/read excel file 2003 (xls) or 2007 (xlsx). We will build web page as below: Step 1: create new spring mvc project as following image:- You need to create 2 files excel 2003 and 2007, example:
- UserInfo.xls: read excel 2003
1 | usernam1 | 10/22/2015 | 2 | usernam2 | 10/23/2015 | 3 | usernam3 | 10/24/2015 |
- UserInfo.xlsx: read excel 2007
1 | usernam1 | 11/22/2015 | 2 | usernam2 | 11/23/2015 | 3 | usernam3 | 11/24/2015 |
Step 2: add org.apache.poi library to read excel and commons-fileupload library use upload excel temporary fileporn.xml: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 | <project xmlns='http://maven.apache.org/POM/4.0.0'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd'> <groupId>com.demo</groupId> <packaging>war</packaging> <name>springmvc Maven Webapp</name> <spring-framework.version>4.1.4.RELEASE</spring-framework.version> <dependency> <artifactId>spring-core</artifactId> </dependency> <groupId>org.springframework</groupId> <version>${spring-framework.version}</version> <!-- excel --> <groupId>org.apache.poi</groupId> <version>3.12</version> <!-- excel 2007 over--> <groupId>org.apache.poi</groupId> <version>3.12</version> <!-- upload file --> <groupId>commons-fileupload</groupId> <version>1.2.2</version> <dependency> <artifactId>commons-io</artifactId> </dependency> <groupId>junit</groupId> <version>3.8.1</version> </dependency> <build> <plugins> <artifactId>maven-compiler-plugin</artifactId> <configuration> <target>1.7</target> </plugin> </build> |
servlet-context.xml: config servlet of spring | <beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:mvc='http://www.springframework.org/schema/mvc' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd'> <context:component-scan base-package='com.demo.springmvc.controller' /> <bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'> <value>/WEB-INF/pages/</value> <property name='suffix'> </property> </beans> |
config-upload.xml: must config upload file to server read file from location temporary of server | <beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xmlns:mvc='http://www.springframework.org/schema/mvc' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd'> <bean id='multipartResolver' class='org.springframework.web.multipart.commons.CommonsMultipartResolver'> <property name='maxUploadSize'value='10240000'/> </beans> |
web.xml: load some files configuration 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | <web-app version='2.5'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet-context.xml</param-value> <load-on-startup>1</load-on-startup> <context-param> <param-value>/WEB-INF/config-upload.xml</param-value> <servlet-mapping> <url-pattern>/</url-pattern> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </web-app> |
Step 3: modify java filesUserInfo.java: entity class to storing information of excel file after read excel 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 | privateStringusername; returnid; publicvoidsetId(intid){ } returnusername; publicvoidsetUsername(Stringusername){ } returninputDate; publicvoidsetInputDate(Date inputDate){ } publicStringtoString(){ } } |
HelloWorldController.java: mange upload and read excel files 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 | importjava.util.List; importjavax.servlet.ServletContext; importorg.apache.poi.hssf.usermodel.HSSFRow; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; importorg.apache.poi.xssf.usermodel.XSSFSheet; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.ui.Model; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RequestParam; importorg.springframework.web.multipart.MultipartFile; importcom.demo.springmvc.entity.UserInfo; @Controller ServletContext context; @RequestMapping(value='/hello',method=RequestMethod.GET) //model.addAttribute('lstUser', lstUser); model.addAttribute('message','Welcome to Spring MVC'); } @RequestMapping(value='/processExcel',method=RequestMethod.POST) publicStringprocessExcel(Model model,@RequestParam('excelfile')MultipartFile excelfile){ List<UserInfo>lstUser=newArrayList<>(); // Creates a workbook object from the uploaded excelfile HSSFWorkbook workbook=newHSSFWorkbook(excelfile.getInputStream()); // Creates a worksheet object representing the first sheet // Reads the data in excel file until last row is encountered // Creates an object for the UserInfo Model // Creates an object representing a single row in excel // Sets the Read data to the model class user.setId((int)row.getCell(0).getNumericCellValue()); user.setUsername(row.getCell(1).getStringCellValue()); user.setInputDate(row.getCell(2).getDateCellValue()); lstUser.add(user); workbook.close(); }catch(Exceptione){ } return'hello'; @RequestMapping(value='/hello',method=RequestMethod.POST) publicStringprocessExcel2003(Model model,@RequestParam('excelfile2007')MultipartFile excelfile){ List<UserInfo>lstUser=newArrayList<>(); // Creates a workbook object from the uploaded excelfile XSSFWorkbook workbook=newXSSFWorkbook(excelfile.getInputStream()); // Creates a worksheet object representing the first sheet // Reads the data in excel file until last row is encountered // Creates an object for the UserInfo Model // Creates an object representing a single row in excel // Sets the Read data to the model class user.setId((int)row.getCell(0).getNumericCellValue()); user.setUsername(row.getCell(1).getStringCellValue()); user.setInputDate(row.getCell(2).getDateCellValue()); lstUser.add(user); workbook.close(); }catch(Exceptione){ } return'hello'; |
Step 4: edit page screen for end usershello.jsp: page import and read excel 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 | <%@pagelanguage='java'contentType='text/html; charset=UTF-8'pageEncoding='UTF-8'%> <%@taglibprefix='c'uri='http://java.sun.com/jsp/jstl/core'%> <%@tagliburi='http://java.sun.com/jsp/jstl/core'prefix='c'%> <%@tagliburi='http://www.springframework.org/tags'prefix='spring'%> <%@tagliburi='http://www.springframework.org/tags/form'prefix='form'%> <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> <head> <meta http-equiv='Content-Type'content='text/html; charset=UTF-8'> </head> <h1>${message}</h1> enctype='multipart/form-data'> <input name='excelfile'type='file'> </form:form> <form:form action='hello'method='post' <div>Excel File 2007:</div> <input type='submit'value='processExcel2007'> <hr> <c:if test='${!empty lstUser}'> <tr> <th width='120'>UserName</th> </tr> <tr> <td>${user.username}</td> </tr> </table> </body> |
index.jsp: redirect root page to hello page 2 | <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <%@tagliburi='http://java.sun.com/jsp/jstl/core'prefix='c'%> |
Done ! Build and run. |