File Upload
|
|
Version 1.0 Reference Documentation
Introduction
The File Upload Tag Library provides an easy way to process files
uploaded from a web form from within JSP pages. It is a tag library
wrapper around the Jakarta Commons FileUpload utility.
Usage Notes
When processing files uploaded from a web form from any Java Servlet
technology, there are a few caveats that must be remembered:
- The body of the request can only be parsed once. Parsing it
consumes it, so there will be nothing to parse if it is attempted
a second time for the same body.
- Since file-upload forms incorporate all the parameters (not just
the file upload ones) into the body of the request and any file
upload parser will consume that body, those parameters will not
be available through the normal 'request' object. Similarly,
parameters will also not be available through the JSTL param
object. The parameters must be accessed through the file-upload
parser.
- Accessing parameters from a file-upload form through the normal
means before parsing will cause the body of the request to be
consumed, which will leave nothing for the file-upload parser
to parse.
With these points in mind, the 'parse' tag of this tag library must
be called exactly once at the beginning of the JSP. This tag will
consume and parse the body of the request and make the files and
parameters available through subsequent access tags.
Once the 'parse' has occurred, parameters can be accessed either
with the 'param' tag or (if using JSTL) through the page-scope map
'param'. Uploaded files can be accessed with the 'file' tag and
several accessor tags.
For example, to get a non-file parameter from a file-upload request:
<fup:parse />
Here is the value of 'firstparam':
<fup:param key="firstparam" />
Here is the value of 'secondparam':
<c:out value="${pageScope.param['secondparam']}" />
To retrieve the size of each uploaded file:
<fup:parse />
<fup:file>
<fup:name /> is <fup:size /> bytes.
</fup:file>
Deployment
To deploy this tag library for use with your web application, follow
these steps:
To use the tags from this library in your JSP pages, add the following
directive at the top of each page:
<%@ taglib uri="/xephyrus-fileupload" prefix="fup" %>
where "fup" is the tag name prefix you wish to use for tags from this
library. You can change this value to any prefix you like.
Example
A simple example of a JSP using the Xephyrus File
Upload Tag Library is available in full syntax-color
highlighting.
View It
Run It
Tag Summary
|
parse
|
Parses parameters and uploaded files.
|
|
param
|
Provides access to non-file parameters.
|
|
file
|
Provides access to file-items that were uploaded
|
|
contents
|
Provides the contents of a file-item.
|
|
contentType
|
Provides the content type (MIME type) of a file-item.
|
|
localPath
|
Provides the client-side path of a file-item.
|
|
name
|
Provides the form-field name of a file-item.
|
|
remotePath
|
Provides the server-side temporary storage path of a file-item.
|
|
size
|
Provides the size in bytes of a file-item.
|
|
write
|
Writes the contents of a file-item to an indicated location.
|
|
delete
|
Removes the temporary storage of a file-item.
|
Tag Reference
parse
The parse tag must be used exactly once per file-upload request
before any parameters or uploaded files are accessed.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
sizeMax
|
java.lang.Long
|
no
|
true
|
1048576 (1MB)
|
|
The maximum allowed upload size, in bytes. If a negative
value is given, no maximum will be imposed.
|
|
sizeThreshold
|
java.lang.Integer
|
no
|
true
|
1024 (1KB)
|
|
The upload size threshold beyond which the uploaded files are
written directly to disk.
|
|
repositoryPath
|
java.lang.String or
java.lang.File
|
no
|
true
|
The system temporary directory.
|
|
The location used to temporarily store the files that are
larger than the configured size threshold.
|
|
nonUpload
|
java.lang.String
|
no
|
true
|
'error'
|
Specifies alternate behavior when the request is not a file-upload
request. Possible values:
- 'error'
Throw a JspTagException bitching and moaning about
it.
- 'ignore'
Quietly do nothing.
- 'pretend'
Import the form parameters as if the request had
been a file-upload request with no files.
|
|
keepEmpty
|
java.lang.String or
java.lang.Boolean
|
no
|
true
|
'false'
|
|
Indicates whether or not to include empty files in the file-item
map. When true, every upload field will result in a file-item
entry in the map. When false, only actually uploaded files will
have a file-item entry in the map.
|
|
paramVar
|
java.lang.String
|
no
|
true
|
param
|
|
The name of the variable to set the mapping of non-file
parameters.
|
|
paramScope
|
java.lang.String
|
no
|
true
|
page
|
|
The scope to set the mapping of non-file parameters.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
fileVar
|
java.lang.String
|
no
|
true
|
fileItem
|
|
The name of the variable to set the mapping of uploaded files.
|
|
fileScope
|
java.lang.String
|
no
|
true
|
page
|
|
The scope to set the mapping of uploaded files.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
param
The param tag provides access to non-file parameters that were part
of the file-upload request processed by the 'parsed' tag.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
map
|
java.lang.String or
java.util.Map
|
no
|
true
|
'param'
|
|
The parameter map. This can refer to either the name of the
parameter map (as specified in the parse tag), or the map itself.
|
|
mapScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the parameter map. This is only valid if the map
attribute is specified and it contains the map name rather than
the map itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
key
|
java.lang.String
|
no
|
true
|
|
|
The name of the parameter to access. If not specified, the set of
keys in this map will be looped over. The key names are not ordered.
|
|
loop
|
java.lang.String or
java.lang.Boolean
|
no
|
true
|
'true'
|
|
Indicates that the body of this tag should be looped over for every
value of the specified key. If no key is specified, the body will
be looped over for every key name. The key names are not ordered.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The variable name to put the parameter value in. If this is not
specified, the parameter value will be sent to the pageContext out.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the parameter value output variable. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
file
The file tag provides access to file-items that were part
of the file-upload request processed by the 'parsed' tag.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
map
|
java.lang.String or
java.util.Map
|
no
|
true
|
'fileItem'
|
|
The file-item map. This can refer to either the name of the
file-item map (as specified in the parse tag), or the map itself.
|
|
mapScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item map. This is only valid if the map
attribute is specified and it contains the map name rather than
the map itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
key
|
java.lang.String
|
no
|
true
|
|
|
The name of the file-item to access. If not specified, the set of
keys in this map will be looped over. The key names are not ordered.
|
|
loop
|
java.lang.String or
java.lang.Boolean
|
no
|
true
|
'true'
|
|
Indicates that the body of this tag should be looped over for every
value of the specified key. If no key is specified, the body will
be looped over for every key name. The key names are not ordered.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The variable name to put the file-item value in. If this is not
specified, the file-item value will be sent to the pageContext out.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item value output variable. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
contents
The contents tag provides access to the contents of the indicated
uploaded file-item that were part of the file-upload request
processed by the 'parsed' tag.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
as
|
java.lang.String
|
no
|
true
|
'string'
|
Indicates how the contents should be provided:
- inputStream
An InputStream object.
- string
A String object.
- byteArray
An array of bytes, e.g. byte[].
|
|
encoding
|
java.lang.String
|
no
|
true
|
system default encoding
|
|
The character encoding to use when converting the contents to a
string. This value is only used if the 'as' attribute specified
'string'. If this is not specified, the default encoding will
be used.
|
contentType
The contentType tag provides access to the content-type of
the indicated uploaded file-item.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
localPath
The localPath tag provides access to the client-side path of the
indicated uploaded file-item.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
as
|
java.lang.String
|
no
|
true
|
'string'
|
Indicates how the path should be provided:
- string
A String object.
- file
A File object.
|
name
The name tag provides access to the submitted form-field name of the
indicated uploaded file-item.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
remotePath
The remotePath tag provides access to the servers temporary storage
path for the indicated uploaded file-item. If the file is smaller
than the specified sizeThreshold (see the 'parse' tag), it will be
held in memory rather than on disk. In that case, this will provide
nothing.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
var
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
as
|
java.lang.String
|
no
|
true
|
'string'
|
Indicates how the path should be provided:
- string
A String object.
- file
A File object.
|
|
keepPath
|
java.lang.String or
java.lang.Boolean
|
no
|
true
|
'true'
|
|
When true, this indicates that the path portion of the remote
filename should be left entact. When false, the path portion
will be removed and only the filename itself will be returned.
Setting this to true does not guarantee that the path
will be available! Many browsers do not send the path.
|
size
The size tag provides access to the size in bytes of the
indicated uploaded file-item.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
write
The write tag writes out the contents of the indicated uploaded
file-item to the file specified. It outputs the full path of the
newly written file to the specified var or the current output.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
var
|
java.lang.String
|
no
|
true
|
|
|
The result variable for this access. If not specified, the result
of this access will be written to the pageContext's current output.
|
|
scope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the result variable for this access. This is only
valid if the var attribute is specified.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
|
to
|
java.lang.String or
java.io.File
|
yes
|
true
|
|
|
Indicates where to write the contents of the file.
|
delete
The delete tag removes the temporary storage of the indicated
uploaded file-item.
|
Attribute
|
Class
|
Required?
|
EL?
|
Default
|
|
Description
|
|
item
|
java.lang.String or
org.apache.commons.fileupload.FileItem
|
no
|
true
|
|
|
Indicates the file-item to access. This can be either the file-item
itself or the variable name of the file-item.
|
|
itemScope
|
java.lang.String
|
no
|
true
|
'page'
|
|
The scope of the file-item to access. This is only valid if the
item attribute is specified and it contains the item name rather
than the item itself.
This can be any scope that the PageContext object recognizes.
Currently that includes:
page, request, session, and application.
|
The Xephyrus File Upload Tag Library is Copyright © 2003
by Topher ZiCornell. All rights reserved.
The Jakarta Commons FileUpload utility is Copyright ©
1999-2003 The Apache Software
Foundation. All rights reserved.
Read the license for licensing
information.
These web pages are Copyright © 2003 by Topher ZiCornell.
All rights reserved.
|