您的位置:首页 > 编程语言 > Java开发

Spring MVC Multiple File Upload example

2014-10-09 13:48 288 查看

Spring MVC Multiple File Upload example

By
Viral 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’s
CommonsMultipartResolver
class internal uses these library to handle uploaded content.

Add following dependencies in your maven based project to add File upload feature.

If you have a simple web application, add following JAR files in WEB-INF/lib folder. You can download all these JARs with source code at the end of this tutorial.



2. Model – Form Object

Create a Java bean which acts as Model/Form object for our Spring application. This bean contains a
List
of
org.springframework.web.multipart.MultipartFile
objects. 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
@ModelAttribute
annotation 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

Note that we defined the file input name as files[0], files[1] etc. This will map the submitted files to the List object correctly.

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 bean
multipartResolver. This will make sure Spring handles the file upload correctly using
CommonsMultipartResolver
class.

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 Beans

Spring 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: