|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.taursys.servlet.HttpMultiPartServletRequest
HttpMultiPartServletRequest is a wrapper/adapter for multipart type requests. This class is used specifically for handling multipart/form-data type resuests (which are not handled by the standard HttpServletRequest API). Multipart type requests are used when a browser submits form data that contains one or more files to upload. When receiving a multipart/form-data request, this class parses the data stream and extracts the form data and uploaded files.
To upload a file using an html form, you must set the
enctype="multipart/form-data"
in the form
tag of
the html. Below is an example of the required html:
<html> <head></head> <body> ... <form enctype="multipart/form-data" method="post" action="MyServeltUrl.sf"> <input type="text" name="color" value="enter your color choice"/> ... <input type="file" name="orderFile"/> <input type="file" name="photo"/> <input type="submit" name="action" value="Send Order"/> </form> </body> </html>
This request wrapper is used by default by the ServletForm to help service an incoming multipart/form-data type request. To use this adapter in a servlet, simply create an instance of this class, set the HttpServletRequest property to the incoming request and invoke the parseRequest method. Then used this class as the request object. Below is an example:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, MultiPartRequestContentException { HttpMultiPartServletRequest rq = new HttpMultiPartServletRequest(request); rq.parseRequest(); String orderXML = rq.getParameter("orderFile"); byte[] photo = rq.getParameterByteArray(""); ...
In addition to the normal request parameters, information about uploaded files as well as the file data itself is available through special parameters. These special parameters names are made up of the input control's name with a suffix of "_FileName" and "_ContentType". For uploaded files with a Content-Type of "text/...", the following parameters are available:
request.getParameter("myFile")
will return the String
contents of the uploaded file.
request.getParameter("myFile_FileName")
will return
"readlist.txt", the name of the uploaded file. (Note: may contain path
information depending on browser).
request.getParameter("myFile_ContentType")
will return
"text/plain", the mime content type of the uploaded file.
request.getParameterByteArray("myFile")
will return a
byte array of the file contents.
request.getParameter("myPicture")
will return the
String value of the uploaded bytes (via new String(bytes[])).
request.getParameter("myPicture_FileName")
will return
"portrait.jpg", the name of the uploaded file. (Note: may contain path
information depending on browser).
request.getParameter("myPicture_ContentType")
will return
"image/jpeg" the mime content type of the uploaded file.
request.getParameterByteArray("myPicture")
will return a
byte array containing the binary data of the picture.
parseRequest
This will parse the request and make all the
parameters and content available. This is automatically invoked by used by a
ServletForm.
get/setmaxFileSize
Controls the maximum allowed size for
an uploaded file. Files beyond this size will cause a MultiPartRequestContentException.
The default size is 1 megabyte.
getParameter(String name)
This method functions as specified
in the servlet API. It will return the String value of the parameter.
getParameterByteArray(String name)
Returns the requested
parameter as an array of bytes rather than a String. No character conversion
occurs.
getParameterByteArrays(String name)
Returns an array of
byte arrays. This is used when multiple files are uploaded with the same
parameter name.
getParameterNames()
Returns an enumeration of parameter
names (as specified in servlet API).
getParameterValues(String name)
Returns a String array of
values for the given parameter. This is used when multiple values are sent
under the same parameter name. (as specified in servlet API).
getReader()
You should NOT invoke this method. It will
always throw an IllegalStateException
.
getRequest
Provides access to the original underlying
HttpServletRequest.
Field Summary | |
static java.lang.String |
BASIC_AUTH
String identifier for Basic authentication. |
static java.lang.String |
CLIENT_CERT_AUTH
String identifier for Basic authentication. |
static java.lang.String |
DIGEST_AUTH
String identifier for Basic authentication. |
static java.lang.String |
FORM_AUTH
String identifier for Basic authentication. |
static java.lang.String |
MULTIPART_FORM_DATA
String identifier for multipart request type. |
static java.lang.String |
PARM_BYTE_ARRAY_SUFFIX
String suffix for byte array parameter. |
static java.lang.String |
PARM_CONTENT_TYPE_SUFFIX
String suffix for file content type. |
static java.lang.String |
PARM_FILE_NAME_SUFFIX
String suffix for file name parameter. |
Constructor Summary | |
HttpMultiPartServletRequest(javax.servlet.http.HttpServletRequest request)
Constructs a new HttpMultiPartServletRequest for the given HttpServletRequest. |
Method Summary | |
java.lang.Object |
getAttribute(java.lang.String name)
Calls same method on the underlying HttpServletRequest. |
java.util.Enumeration |
getAttributeNames()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getAuthType()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getBoundary()
Unit testing method only - do not use. |
java.lang.String |
getCharacterEncoding()
Calls same method on the underlying HttpServletRequest. |
int |
getContentLength()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getContentType()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getContextPath()
Calls same method on the underlying HttpServletRequest. |
javax.servlet.http.Cookie[] |
getCookies()
Calls same method on the underlying HttpServletRequest. |
long |
getDateHeader(java.lang.String name)
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getHeader(java.lang.String name)
Calls same method on the underlying HttpServletRequest. |
java.util.Enumeration |
getHeaderNames()
Calls same method on the underlying HttpServletRequest. |
java.util.Enumeration |
getHeaders(java.lang.String name)
Calls same method on the underlying HttpServletRequest. |
javax.servlet.ServletInputStream |
getInputStream()
Retrieves the body of the request as binary data using a ServletInputStream . |
int |
getIntHeader(java.lang.String name)
Calls same method on the underlying HttpServletRequest. |
java.util.Locale |
getLocale()
Calls same method on the underlying HttpServletRequest. |
java.util.Enumeration |
getLocales()
Calls same method on the underlying HttpServletRequest. |
int |
getMaxFileSize()
Get the maximum file size which can be uploaded. |
int |
getMaxLineLength()
Get the maximum length for a single line. |
java.lang.String |
getMethod()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getParameter(java.lang.String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist. |
byte[] |
getParameterByteArray(java.lang.String name)
Returns the binary value of a request parameter as a byte
array, or null if the parameter does not exist. |
java.lang.Object[] |
getParameterByteArrays(java.lang.String name)
Returns an array of byte arrays containing all of the
binary file data the given request parameter has, or null if the
parameter does not exist. |
java.util.Map |
getParameterMap()
Returns a java.util.Map of the parameters of this request. |
java.util.Enumeration |
getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request. |
java.lang.String[] |
getParameterValues(java.lang.String name)
Returns an array of String objects containing all of the
values the given request parameter has, or null if the
parameter does not exist. |
java.lang.String |
getPathInfo()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getPathTranslated()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getProtocol()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getQueryString()
Calls same method on the underlying HttpServletRequest. |
java.io.BufferedReader |
getReader()
Do not invoke this method. |
java.lang.String |
getRealPath(java.lang.String path)
Deprecated. |
java.lang.String |
getRemoteAddr()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getRemoteHost()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getRemoteUser()
Calls same method on the underlying HttpServletRequest. |
javax.servlet.http.HttpServletRequest |
getRequest()
Get the underlying HttpServletRequest for this object. |
javax.servlet.RequestDispatcher |
getRequestDispatcher(java.lang.String path)
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getRequestedSessionId()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getRequestURI()
Calls same method on the underlying HttpServletRequest. |
java.lang.StringBuffer |
getRequestURL()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getScheme()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getServerName()
Calls same method on the underlying HttpServletRequest. |
int |
getServerPort()
Calls same method on the underlying HttpServletRequest. |
java.lang.String |
getServletPath()
Calls same method on the underlying HttpServletRequest. |
javax.servlet.http.HttpSession |
getSession()
Calls same method on the underlying HttpServletRequest. |
javax.servlet.http.HttpSession |
getSession(boolean create)
Calls same method on the underlying HttpServletRequest. |
java.security.Principal |
getUserPrincipal()
Calls same method on the underlying HttpServletRequest. |
boolean |
isEndOfData()
Unit testing method only - do not use. |
boolean |
isRequestedSessionIdFromCookie()
Calls same method on the underlying HttpServletRequest. |
boolean |
isRequestedSessionIdFromUrl()
Deprecated. |
boolean |
isRequestedSessionIdFromURL()
Calls same method on the underlying HttpServletRequest. |
boolean |
isRequestedSessionIdValid()
Calls same method on the underlying HttpServletRequest. |
boolean |
isSecure()
Calls same method on the underlying HttpServletRequest. |
boolean |
isUserInRole(java.lang.String role)
Calls same method on the underlying HttpServletRequest. |
static void |
main(java.lang.String[] args)
For testing purposes only. |
void |
parseRequest()
Parse the servlet request and extract parameter information. |
void |
processBlock()
Unit testing method only - do not use. |
int |
readChars(char[] results,
int offset,
java.lang.String key)
|
void |
removeAttribute(java.lang.String name)
Calls same method on the underlying HttpServletRequest. |
void |
setAttribute(java.lang.String name,
java.lang.Object o)
Calls same method on the underlying HttpServletRequest. |
void |
setCharacterEncoding(java.lang.String encoding)
Calls same method on the underlying HttpServletRequest. |
void |
setMaxFileSize(int newMaxFileSize)
Set the maximum file size which can be uploaded. |
void |
setMaxLineLength(int newMaxLineLength)
Set the maximum length for a single line. |
void |
setupBoundary()
Unit testing method only - do not use. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String PARM_BYTE_ARRAY_SUFFIX
public static final java.lang.String PARM_FILE_NAME_SUFFIX
public static final java.lang.String PARM_CONTENT_TYPE_SUFFIX
public static final java.lang.String BASIC_AUTH
public static final java.lang.String FORM_AUTH
public static final java.lang.String CLIENT_CERT_AUTH
public static final java.lang.String DIGEST_AUTH
public static final java.lang.String MULTIPART_FORM_DATA
Constructor Detail |
public HttpMultiPartServletRequest(javax.servlet.http.HttpServletRequest request)
parseRequest()
method before using this
class.
Method Detail |
public void parseRequest() throws MultiPartRequestSizeException, MultiPartRequestContentException, java.io.IOException
MultiPartRequestSizeException
- if max line length or file size exceeded
MultiPartRequestContentException
- if invalid data received
java.io.IOException
- if problem reading data streampublic void setupBoundary() throws MultiPartRequestContentException, java.io.IOException
MultiPartRequestContentException
- if ContentType is not multipart/form-data
java.io.IOException
public void processBlock() throws MultiPartRequestSizeException, MultiPartRequestContentException, java.io.IOException
MultiPartRequestSizeException
MultiPartRequestContentException
java.io.IOException
public java.util.Enumeration getParameterNames()
getParameterNames
in interface javax.servlet.ServletRequest
public java.lang.String getParameter(java.lang.String name)
You should only use this method when you are sure the parameter has only
one value. If the parameter might have more than one value, use
getParameterValues(java.lang.String).
If you use this method with a multivalued parameter, the value returned
is equal to the first value in the array returned by
getParameterValues
.
If the parameter data was sent in the request body, such as occurs with
an HTTP POST request, then reading the body directly via
getInputStream()
or getReader()
can interfere
with the execution of this method.
getParameter
in interface javax.servlet.ServletRequest
name
- - a String specifying the name of the parameter
public java.lang.String[] getParameterValues(java.lang.String name)
String
objects containing all of the
values the given request parameter has, or null
if the
parameter does not exist.
If the parameter has a single value, the array has a length of 1.
getParameterValues
in interface javax.servlet.ServletRequest
name
- - a String containing the name of the parameter whose value
is requested
String
objects containing the
parameter's valuespublic java.util.Map getParameterMap()
java.util.Map
of the parameters of this request.
Request parameters are extra information sent with the request. For HTTP
servlets, parameters are contained in the query string or posted form data.
getParameterMap
in interface javax.servlet.ServletRequest
java.util.Map
containing parameter
names as keys and parameter values as map values. The keys in the
parameter map are of type String. The values in the parameter map are
of type Object array.public byte[] getParameterByteArray(java.lang.String name)
byte
array, or null if the parameter does not exist.
The parameter name MAY end with "_ByteArray" (eg "myFile_ByteArray").
You should only use this method when you are sure the parameter has only
one value. If the parameter might have more than one value, use
getParameterByteArrays(java.lang.String).
If you use this method with a multivalued parameter, the value returned
is equal to the first value in the array returned by
getParameterByteArrays
.
name
- - a String specifying the name of the parameter
byte
array of the binary file data for the parameter.public java.lang.Object[] getParameterByteArrays(java.lang.String name)
byte
arrays containing all of the
binary file data the given request parameter has, or null if the
parameter does not exist.
If the parameter has a single value, the array has a length of 1.
name
- - a String specifying the name of the parameter
byte
arrays of the binary file data for
the parameter.public javax.servlet.ServletInputStream getInputStream() throws java.io.IOException
ServletInputStream
. This method caches the request stream
after the first call.
getInputStream
in interface javax.servlet.ServletRequest
ServletInputStream
object containing the body of
the request
java.lang.IllegalStateException
- if the getReader()
method has
already been called on the underlying request object.
java.io.IOException
- if an input or output exception occurredpublic java.io.BufferedReader getReader() throws java.io.IOException
getInputStream()
method, which will invalidate the use of
this method. This method will always throw an
IllegalStateException
getReader
in interface javax.servlet.ServletRequest
java.io.IOException
- is never thrown in this implementation
java.lang.IllegalStateException
- whenever this method is invoked.public javax.servlet.http.HttpServletRequest getRequest()
HttpServletRequest
for this object.
HttpServletRequest
for this object.public java.lang.String getBoundary()
public boolean isEndOfData()
parseRequest()
has been invoked.public void setMaxFileSize(int newMaxFileSize)
ArraySubscriptException
. The default size
is 1 megabyte.
newMaxFileSize
- the maximum file size which can be uploaded.public int getMaxFileSize()
ArraySubscriptException
. The default size
is 1 megabyte.
public void setMaxLineLength(int newMaxLineLength)
ArraySubscriptException
. The default size is 4,096 bytes.
newMaxLineLength
- the maximum length for a single line.public int getMaxLineLength()
ArraySubscriptException
. The default size is 4,096 bytes.
public int readChars(char[] results, int offset, java.lang.String key) throws MultiPartRequestSizeException, MultiPartRequestContentException, java.io.IOException
MultiPartRequestSizeException
MultiPartRequestContentException
java.io.IOException
public java.lang.String getAuthType()
getAuthType
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.Cookie[] getCookies()
getCookies
in interface javax.servlet.http.HttpServletRequest
public long getDateHeader(java.lang.String name)
getDateHeader
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getHeader(java.lang.String name)
getHeader
in interface javax.servlet.http.HttpServletRequest
public java.util.Enumeration getHeaders(java.lang.String name)
getHeaders
in interface javax.servlet.http.HttpServletRequest
public java.util.Enumeration getHeaderNames()
getHeaderNames
in interface javax.servlet.http.HttpServletRequest
public int getIntHeader(java.lang.String name)
getIntHeader
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getMethod()
getMethod
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getPathInfo()
getPathInfo
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getPathTranslated()
getPathTranslated
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getContextPath()
getContextPath
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getQueryString()
getQueryString
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getRemoteUser()
getRemoteUser
in interface javax.servlet.http.HttpServletRequest
public boolean isUserInRole(java.lang.String role)
isUserInRole
in interface javax.servlet.http.HttpServletRequest
public java.security.Principal getUserPrincipal()
getUserPrincipal
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getRequestedSessionId()
getRequestedSessionId
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getRequestURI()
getRequestURI
in interface javax.servlet.http.HttpServletRequest
public java.lang.String getServletPath()
getServletPath
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession(boolean create)
getSession
in interface javax.servlet.http.HttpServletRequest
public javax.servlet.http.HttpSession getSession()
getSession
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdValid()
isRequestedSessionIdValid
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromCookie()
isRequestedSessionIdFromCookie
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromURL()
isRequestedSessionIdFromURL
in interface javax.servlet.http.HttpServletRequest
public boolean isRequestedSessionIdFromUrl()
isRequestedSessionIdFromUrl
in interface javax.servlet.http.HttpServletRequest
public java.lang.Object getAttribute(java.lang.String name)
getAttribute
in interface javax.servlet.ServletRequest
public java.util.Enumeration getAttributeNames()
getAttributeNames
in interface javax.servlet.ServletRequest
public java.lang.String getCharacterEncoding()
getCharacterEncoding
in interface javax.servlet.ServletRequest
public int getContentLength()
getContentLength
in interface javax.servlet.ServletRequest
public java.lang.String getContentType()
getContentType
in interface javax.servlet.ServletRequest
public java.lang.String getProtocol()
getProtocol
in interface javax.servlet.ServletRequest
public java.lang.String getScheme()
getScheme
in interface javax.servlet.ServletRequest
public java.lang.String getServerName()
getServerName
in interface javax.servlet.ServletRequest
public int getServerPort()
getServerPort
in interface javax.servlet.ServletRequest
public java.lang.String getRemoteAddr()
getRemoteAddr
in interface javax.servlet.ServletRequest
public java.lang.String getRemoteHost()
getRemoteHost
in interface javax.servlet.ServletRequest
public void setAttribute(java.lang.String name, java.lang.Object o)
setAttribute
in interface javax.servlet.ServletRequest
public void removeAttribute(java.lang.String name)
removeAttribute
in interface javax.servlet.ServletRequest
public java.util.Locale getLocale()
getLocale
in interface javax.servlet.ServletRequest
public java.util.Enumeration getLocales()
getLocales
in interface javax.servlet.ServletRequest
public boolean isSecure()
isSecure
in interface javax.servlet.ServletRequest
public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
getRequestDispatcher
in interface javax.servlet.ServletRequest
public java.lang.String getRealPath(java.lang.String path)
getRealPath
in interface javax.servlet.ServletRequest
public java.lang.StringBuffer getRequestURL()
getRequestURL
in interface javax.servlet.http.HttpServletRequest
public void setCharacterEncoding(java.lang.String encoding) throws java.io.UnsupportedEncodingException
setCharacterEncoding
in interface javax.servlet.ServletRequest
java.io.UnsupportedEncodingException
public static void main(java.lang.String[] args)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |