Expert Spring MVC and Web Flow

(Dana P.) #1

Listing 5-39.MultipartHttpServletRequest Interface


package org.springframework.web.multipart;


public interface MultipartHttpServletRequest extends HttpServletRequest {


Iterator getFileNames();

MultipartFile getFile(String name);

Map getFileMap();

}


Before the end of the request life cycle and after the handling code has had a chance to
work with the uploaded files, the DispatcherServletwill then call cleanupMultipart(). This
removes any state left behind by the file upload implementation code, such as temporary files
on the file system. Therefore, it is important that any request handling code should work with
the uploaded files before request processing finishes.
So which library should you use, Commons’ FileUpload or COS? The choice is up to you,
as both have been around for years and are considered stable. However, keep in mind that
Commons’ FileUpload will probably receive more maintenance in the future. Of course, if
neither provides the features you require, you may implement a new MultipartResolver.


Example


Working with file uploads is actually quite simple, as most of the mechanisms are handled by
the DispatcherServletand thus hidden from request handling code. For an example, we will
register a Jakarta Commons FileUpload MultipartResolverand create a Controllerthat saves
uploaded files to a temporary directory.
Listing 5-40 contains the configuration required for the CommonsMultipartResolver.


Listing 5-40.MultipartResolver ApplicationContext


<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">



<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2000000" />
</bean>

<bean name="/handleUpload"
class="com.apress.expertspringmvc.chap4.HandleUploadController">
<property name="tempDirectory" value="/tmp" />

CHAPTER 5 ■THE PROCESSING PIPELINE 109
Free download pdf