Spring MVC Multiple File Upload example
2014-10-09 13:48
288 查看
Spring MVC Multiple File Upload example
ByViral Patel on November 7, 2012
In this simple tutorial we will see how to implement multiple file upload in a Spring 3 MVC based application.
The requirement is simple. We have a form which displays file input component. User selects a file and upload it. Also its possible to add more file input components using Add button. Once the files are selected and uploaded, the file names are displayed
on success page.
1. Maven Dependencies / Required JAR files
If you using Maven in your project for dependency management, you’ll need to add dependencies for Apache Common File upload and Apache Common IO libraries. The spring’sCommonsMultipartResolverclass internal uses these library to handle uploaded content.
Add following dependencies in your maven based project to add File upload feature.
2. Model – Form Object
Create a Java bean which acts as Model/Form object for our Spring application. This bean contains aListof
org.springframework.web.multipart.MultipartFileobjects. Spring framework provides a useful class MultipartFile which can be used to fetch the file content of uploaded file. Apart from its content, the MultipartFile object
also gives you other useful information such as filename, file size etc.
FileUploadForm.java
3. Controller – Spring Controller
Create a Spring 3 MVC based controller which handles file upload. There are two methods in this controller:displayForm– Is a used to show input form to user. It simply forwards to the page file_upload_form.jsp
save– Fetches the form using
@ModelAttributeannotation and get the File content from it. It creates a list of filenames of files being uploaded and pass this list to success page.
FileUploadController.java
4. View – JSP views
Now create the view pages for this application. We will need two JSPs, one to display file upload form and another to show result on successful upload.The file_upload_form.jsp displays a form with file input. Apart from this we have added small jquery snippet onclick of Add button. This will add a new file input component at the end of form. This allows user to upload as many files as they want (subjected
to file size limit ofcourse).
Note that we have set enctype=”multipart/form-data” attribute of our <form> tag.
file_upload_form.jsp
I would suggest you to go through this tutorial to understand how Spring maps multiple entries from form to bean:
Multiple Row Form Submit using List of Beans
Second view page is to display filename of uploaded file. It simply loops through filename list and display the names.
file_upload_success.jsp
5. Spring Configuration
In Spring configuration (spring-servlet.xml) we define several important configurations. Note how we defined beanmultipartResolver. This will make sure Spring handles the file upload correctly using
CommonsMultipartResolverclass.
spring-servlet.xml
6. Output
Execute the project in Eclipse. Open following URL in browser to see file upload form.URL: http://localhost:8080/Spring3MVC_Multiple_File_Upload_example/show.html
Select files through file dialog and press Upload button to upload. Following page will displayed with list of files being uploaded.
We have added a small Javascript snippet for Add button. This will add more file upload components to the page. Use this if you want to upload more files.
Download Source Code
SpringMVC_Multi_File_Upload_example.zip (3.6 MB)Related Articles
Spring MVC: Multiple Row Form Submit using List of BeansSpring 3 MVC: Handling Forms in Spring 3.0 MVC
Spring @RequestHeader Annotation example
Spring 3 MVC: Create Hello World application in Spring
3.0 MVC
Spring MVC Flash Attribute tutorial with example
Spring MVC HashMap Form Integration example
Spring 3 MVC Interceptor tutorial with example
相关文章推荐
- Spring MVC File Upload Example Tutorial – Single and Multiple Files
- plupload -- multiple file upload & Spring MVC
- ExtJS 4 File Upload + Spring MVC 3 Example
- Spring MVC + Mybatis+MySQL File Upload and Download Example
- springMVC + ajaxfileupload异步上传图片预览,裁剪并保存图片
- springmvc+ajaxFileUpload上传文件(前后台彻底分离的情况下)
- spring mvc multipart file upload
- springmvc+ajaxfileupload实现头像上传并预览
- SpringMVC+ajaxfileupload.js实现文件无刷新上传
- SpringMVC+ajaxFileUpload 异步上传
- SpringMVC+ajaxFileUpload上传图片 IE浏览器弹下载框问题解决方案
- springMVC + ajaxfileupload异步上传图片预览,裁剪并保存图片
- SpringMVC + ajaxfileupload的多文件上传
- springmvc + ajaxFileUpload多文件上传
- SpringMvc + Freemarker + JSON + FileUpload + Log4j + Hibernate + c3p0 + oscache
- SpringMVC+ajaxfileupload上传
- SpringMVC+ajaxFileUpload上传图片 IE浏览器弹下载框问题解决方案
- Bootstrap FileInput.js + springmvc uploadFile
- SpringMVC+ajaxFileUpload 上传文件
- spring mvc upload file exmaple