img
. .
http://commons.apache.org/fileupload) to serve this purpose. Spring MVC has built-in support for
Commons FileUpload. However, from Servlet 3.0, file upload has become a built-in feature of the web
container. Tomcat 7 supports Servlet 3.0, and Spring 3.1 also added support for Servlet 3.0 file upload.
In the following sections, we will discuss how to implement the file upload using Spring MVC 3.1
and Servlet 3.0.
To support file upload, we need one more dependency, and we also need to revise the dependency
for Servlet API versions 2.5 to 3.0. Table 17-7 shows the dependency that requires deletion and the two
new dependencies we need to add.
Table 17-7. Maven Dependencies for File Upload
Group ID
Artifact ID
Version
Description
2.5
Delete this dependency.
javax.servlet
servlet-api
6.0
JEE 6.0 Web Profile API, which contains the
javax
javaee-web-api
library for Servlet 3.0. Please use the
provided scope for this dependency.
2.1
The Apache Commons IO module provides a
commons-io
commons-io
lot of useful functions to ease I/O handling
in Java.
Configuring File Upload Support
In a Servlet 3.0­compatible web container with Spring MVC, configuring file upload support is a two-
step process.
First, in the web deployment descriptor (web.xml) for the DispatchServlet definition, we need to add
a <multipart-config> section. Listing 17-52 shows the code snippet for this change.
Listing 17-52. Add File Upload Support in web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
<!-- Other code omitted -->
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<multipart-config>
<max-file-size>5000000</max-file-size>
</multipart-config>
</servlet>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home