Java Reference
In-Depth Information
Chapter 8. Uploading Files with Java Servlet Technology
Supporting file uploads is a very basic and common requirement for many web applica-
tions. Prior to Servlet 3.0, implementing file upload required the use of external libraries or
complex input processing. Version 3.0 of the Java Servlet specification helps to provide a
viable solution to the problem in a generic and portable way. The Servlet 3.0 specification
supports file upload out of the box, so any web container that implements the specification
can parse multipart requests and make mime attachments available through the HttpSer-
vletRequest object.
A new annotation, javax.servlet.annotation.MultipartConfig , is used to
indicate that the servlet on which it is declared expects requests to made using the multi-
part/form-data MIME type. Servlets that are annotated with @MultipartConfig
can retrieve the Part components of a given multipart/form-data request by call-
ing the request.getPart(String name) or request.getParts() method.
The following topics are addressed here:
• “ The @MultipartConfig Annotation on page 175
• “ The getParts and getPart Methods on page 176
• “ The fileupload Example Application on page 177
The @MultipartConfig Annotation
The @MultipartConfig annotation supports the following optional attributes:
location : An absolute path to a directory on the file system. The location at-
tribute does not support a path relative to the application context. This location is
used to store files temporarily while the parts are processed or when the size of the
file exceeds the specified fileSizeThreshold setting. The default location is
“”.
fileSizeThreshold : The file size in bytes after which the file will be tempor-
arily stored on disk. The default size is 0 bytes.
MaxFileSize : The maximum size allowed for uploaded files, in bytes. If the size
of any uploaded file is greater than this size, the web container will throw an excep-
tion ( IllegalStateException ). The default size is unlimited.
Search WWH ::




Custom Search