de.opus5.servlet
Class MultipartRequest

java.lang.Object
  |
  +--de.opus5.servlet.MultipartRequest

public class MultipartRequest
extends java.lang.Object
implements javax.servlet.http.HttpServletRequest

A class for handling multipart form uploads. This version is for JSDK 2.2. de.opus5.serlvet.* handles multipart requests , which means you can use this library for file uploads. The MultipartRequest implements javax.servlet.htt.HttpServletRequest, so you can use the HttpServletRequest-API for multipart requests. There are some extensions for access to the uploaded files. The MultipartRequest class can store small uploads in main memory, if you want it to (see constructors for details). Access to the uploaded files is provided via the UploadedFile class, no mather if the files are temporary stored in main memory or disk. Access to the file data is provided via java streams, so you can easily write the file data wherever you like (ie directly to a database). A ready to use precompiled version is included in this archive. Note that this is free software and under the LGPL-license (see LICENCE file). Mail bugs, improovments, comments to mailto:frederik.dahlke@web.de

Example:

 MulipartRequest req;
 ..
 try {
    req = new MultipartRequest(servletRequest,maxMemoryStoredRequestSize,
			tmpdir,fileprefix);
 } catch (Exception e) {
   ...
 }

 ....
 filename = req.getUploadedFile("Document").getName();
 is = req.getInputStreamForFile("Document");
 while (is.read()...) {
 ...
 }
 login = req.getParameter("Login");
 ...
 UploadedFile adressFile = mReq.getUploadedFile(Global.MPADRESSF_KEY);
 UploadedFile docFile = mReq.getUploadedFile(Global.MPDOCFILE_KEY);
 myObject.setAdressFile(adressFile); 
 ....
 ....
 

Version:
0.1.6
Author:
Frederik Dahlke, frederik.dahlke@web.de

Inner Class Summary
 class MultipartRequest.BucketHash
          Simple class for storing parameters
 
Constructor Summary
MultipartRequest(javax.servlet.http.HttpServletRequest sR, int maxMemStoredReqSiz, int maxReqSiz, java.lang.String tmpDir, java.lang.String uniquePrefix)
          Create a MultipartRequest, temporary files will be deleted.
MultipartRequest(javax.servlet.http.HttpServletRequest sR, int maxMemStoredReqSiz, java.lang.String tmpDir, java.lang.String uniquePrefix)
          Create a MultipartRequest, temporary files will be deleted, the maximum request size is set to 1MB.
MultipartRequest(javax.servlet.http.HttpServletRequest sR, int maxReqSiz, java.lang.String tmpDir, java.lang.String uniquePrefix, boolean keep)
          Create a MultipartRequest, files will be saved to disk allways.
MultipartRequest(javax.servlet.http.HttpServletRequest sR, java.lang.String tmpDir, java.lang.String uniquePrefix)
          Create a MultipartRequest using the HttpServletRequest object, temporary files will be deleted.
 
Method Summary
 java.lang.Object getAttribute(java.lang.String name)
          Calls the corresponding method in ServletRequest-object.
 java.util.Enumeration getAttributeNames()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getAuthType()
          Returns the authentication scheme of the request, or null if none.
 java.lang.String getCharacterEncoding()
          Calls the corresponding method in ServletRequest-object.
 int getContentLength()
          Returns the size of the request entity data, or -1 if not known.
 java.lang.String getContentType()
           
 java.lang.String getContextPath()
          Returns the portion of the request URI that indicates the context of the request.
 javax.servlet.http.Cookie[] getCookies()
          Gets the array of cookies found in this request.
 long getDateHeader(java.lang.String name)
          Returns the value of a date header field, or -1 if not found.
 java.io.File getFile(java.lang.String fieldname)
          returns a java.io.File object if the file was saved on disk, null otherwise.
 java.lang.String getHeader(java.lang.String name)
          Returns the value of a header field, or null if not known.
 java.util.Enumeration getHeaderNames()
          Returns an enumeration of strings representing the header names for this request.
 java.util.Enumeration getHeaders(java.lang.String name)
          Returns all the values of the specified request header as an Enumeration of String objects.
 javax.servlet.ServletInputStream getInputStream()
          Calls the corresponding method in ServletRequest-object.
 java.io.InputStream getInputStreamForFile(java.lang.String fieldname)
          return an InputStream for reading an uploaded file.
 int getIntHeader(java.lang.String name)
          Returns the value of an integer header field, or -1 if not found.
 java.util.Locale getLocale()
          Calls the corresponding method in ServletRequest-object.
 java.util.Enumeration getLocales()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getMethod()
          Returns the method with which the request was made.
 java.lang.String getParameter(java.lang.String name)
          return the value of parameter with specified name
 java.util.Enumeration getParameterNames()
          return the names of all parameters as Enumeration
 java.lang.String[] getParameterValues(java.lang.String name)
          return the value of the specified parameter as an array of strings.
 java.lang.String getPathInfo()
           
 java.lang.String getPathTranslated()
          Returns extra path information translated to a real path.
 java.lang.String getProtocol()
          Returns the protocol and version of the request as a string of the form <protocol>/<major version>.<minor version>.
 java.lang.String getQueryString()
          Returns the query string part of the presentation URI, or null if none.
 java.io.BufferedReader getReader()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getRealPath(java.lang.String path)
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getRemoteAddr()
          Returns the IP address of the agent that sent the request.
 java.lang.String getRemoteHost()
          Returns the fully qualified host name of the agent that sent the request.
 java.lang.String getRemoteUser()
          Returns the name of the user making this request, or null if not known.
 javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
          Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
 java.lang.String getRequestedSessionId()
           
 java.lang.String getRequestURI()
          Returns the request URI.
 java.lang.String getScheme()
          Returns the scheme of the URL used in this request, for example "http", "https", or "ftp".
 java.lang.String getServerName()
          Returns the host name of the server that received the request.
 int getServerPort()
          Returns the port number on which this request was received.
 java.lang.String getServletPath()
           
 javax.servlet.http.HttpSession getSession()
           
 javax.servlet.http.HttpSession getSession(boolean create)
           
 UploadedFile getUploadedFile(java.lang.String fieldname)
          Get the UploadedFile object for the uploaded file with given name.
 java.util.Enumeration getUploadedFiles()
          Get an enumeration of the UploadedFile objects.
 java.security.Principal getUserPrincipal()
           
 boolean isRequestedSessionIdFromCookie()
           
 boolean isRequestedSessionIdFromUrl()
           
 boolean isRequestedSessionIdFromURL()
           
 boolean isRequestedSessionIdValid()
           
 boolean isSecure()
          Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
 boolean isUserInRole(java.lang.String role)
           
 void removeAttribute(java.lang.String name)
          Removes an attribute from this request.
 void setAttribute(java.lang.String name, java.lang.Object o)
          Stores an attribute in this request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultipartRequest

public MultipartRequest(javax.servlet.http.HttpServletRequest sR,
                        int maxMemStoredReqSiz,
                        int maxReqSiz,
                        java.lang.String tmpDir,
                        java.lang.String uniquePrefix)
                 throws java.lang.IllegalArgumentException,
                        java.io.IOException
Create a MultipartRequest, temporary files will be deleted.
Parameters:
sR - the HttpServletRequest
maxMemStoredReqSiz - If the request size in bytes exceeds this value, the uploaded files will be stored on disk.
maxReqSiz - Maximum size of request.
tmpDir - Directory where temporary files are saved.
uniquePrefix - Prefix added to the filenames to prevent overwriting of files with the same name.

MultipartRequest

public MultipartRequest(javax.servlet.http.HttpServletRequest sR,
                        int maxMemStoredReqSiz,
                        java.lang.String tmpDir,
                        java.lang.String uniquePrefix)
                 throws java.lang.IllegalArgumentException,
                        java.io.IOException
Create a MultipartRequest, temporary files will be deleted, the maximum request size is set to 1MB.
Parameters:
sR - the HttpServletRequest
maxMemStoredReqSiz - If the request size in bytes exceeds this value, the uploaded files will be stored on disk.
tmpDir - Directory where temporary files are saved.
uniquePrefix - Prefix added to the filenames to prevent overwriting of files with the same name.

MultipartRequest

public MultipartRequest(javax.servlet.http.HttpServletRequest sR,
                        int maxReqSiz,
                        java.lang.String tmpDir,
                        java.lang.String uniquePrefix,
                        boolean keep)
                 throws java.lang.IllegalArgumentException,
                        java.io.IOException
Create a MultipartRequest, files will be saved to disk allways.
Parameters:
sR - the HttpServletRequest
maxReqSiz - Maximum size of request.
tmpDir - Directory where temporary files are saved.
uniquePrefix - Prefix added to the filenames to prevent overwriting of files with the same name.
keep - If set, the uploaded files will not be deleted.

MultipartRequest

public MultipartRequest(javax.servlet.http.HttpServletRequest sR,
                        java.lang.String tmpDir,
                        java.lang.String uniquePrefix)
                 throws java.lang.IllegalArgumentException,
                        java.io.IOException
Create a MultipartRequest using the HttpServletRequest object, temporary files will be deleted. Maximum request size is 1MB, files will be saved to disk.
Parameters:
sR - the HttpServletRequest
tmpDir - Directory where temporary files are saved.
uniquePrefix - Prefix added to the filenames to prevent overwriting of files with the same name.
Method Detail

getFile

public java.io.File getFile(java.lang.String fieldname)
returns a java.io.File object if the file was saved on disk, null otherwise. This this method should only be called, if keepFiles is set to true.
Parameters:
fieldname - the form parameter field name
Returns:
the java.io.File object

getInputStreamForFile

public java.io.InputStream getInputStreamForFile(java.lang.String fieldname)
                                          throws java.io.IOException
return an InputStream for reading an uploaded file.
Parameters:
filename - the form parameter field name
Returns:
InputStream for reading the filedata.

getParameterNames

public java.util.Enumeration getParameterNames()
return the names of all parameters as Enumeration
Returns:
Enumeration of parameter names

getParameter

public java.lang.String getParameter(java.lang.String name)
return the value of parameter with specified name
Parameters:
name - the parameter name
Returns:
the parameter value

getParameterValues

public java.lang.String[] getParameterValues(java.lang.String name)
return the value of the specified parameter as an array of strings.

getUploadedFile

public UploadedFile getUploadedFile(java.lang.String fieldname)
Get the UploadedFile object for the uploaded file with given name.
Parameters:
fieldname - the form parameter field name

getUploadedFiles

public java.util.Enumeration getUploadedFiles()
Get an enumeration of the UploadedFile objects.

getContentLength

public int getContentLength()
Returns the size of the request entity data, or -1 if not known. Same as the CGI variable CONTENT_LENGTH.

getContentType

public java.lang.String getContentType()

getProtocol

public java.lang.String getProtocol()
Returns the protocol and version of the request as a string of the form <protocol>/<major version>.<minor version>. Same as the CGI variable SERVER_PROTOCOL.

getScheme

public java.lang.String getScheme()
Returns the scheme of the URL used in this request, for example "http", "https", or "ftp". Different schemes have different rules for constructing URLs, as noted in RFC 1738. The URL used to create a request may be reconstructed using this scheme, the server name and port, and additional information such as URIs.

getServerName

public java.lang.String getServerName()
Returns the host name of the server that received the request. Same as the CGI variable SERVER_NAME.

isSecure

public boolean isSecure()
Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.

removeAttribute

public void removeAttribute(java.lang.String name)
Removes an attribute from this request.

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object o)
Stores an attribute in this request.

getServerPort

public int getServerPort()
Returns the port number on which this request was received. Same as the CGI variable SERVER_PORT.

getRemoteAddr

public java.lang.String getRemoteAddr()
Returns the IP address of the agent that sent the request. Same as the CGI variable REMOTE_ADDR.

getRemoteHost

public java.lang.String getRemoteHost()
Returns the fully qualified host name of the agent that sent the request. Same as the CGI variable REMOTE_HOST.

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

getMethod

public java.lang.String getMethod()
Returns the method with which the request was made. The returned value can be "GET", "HEAD", "POST", or an extension method. Same as the CGI variable REQUEST_METHOD.
Specified by:
getMethod in interface javax.servlet.http.HttpServletRequest

getRequestURI

public java.lang.String getRequestURI()
Returns the request URI.
Specified by:
getRequestURI in interface javax.servlet.http.HttpServletRequest

getPathTranslated

public java.lang.String getPathTranslated()
Returns extra path information translated to a real path. Returns null if no extra path information specified. Same as the CGI variable PATH_TRANSLATED.
Specified by:
getPathTranslated in interface javax.servlet.http.HttpServletRequest

getQueryString

public java.lang.String getQueryString()
Returns the query string part of the presentation URI, or null if none. Same as the CGI variable QUERY_STRING.
Specified by:
getQueryString in interface javax.servlet.http.HttpServletRequest

getRemoteUser

public java.lang.String getRemoteUser()
Returns the name of the user making this request, or null if not known. The user name is set with HTTP authentication. Whether the user name will continue to be sent with each subsequent communication is browser-dependent. Same as the CGI variable REMOTE_USER.
Specified by:
getRemoteUser in interface javax.servlet.http.HttpServletRequest

getAuthType

public java.lang.String getAuthType()
Returns the authentication scheme of the request, or null if none. Same as the CGI variable AUTH_TYPE.
Specified by:
getAuthType in interface javax.servlet.http.HttpServletRequest

getContextPath

public java.lang.String getContextPath()
Returns the portion of the request URI that indicates the context of the request.
Specified by:
getContextPath in interface javax.servlet.http.HttpServletRequest

getCookies

public javax.servlet.http.Cookie[] getCookies()
Gets the array of cookies found in this request.
Specified by:
getCookies in interface javax.servlet.http.HttpServletRequest
Returns:
The array of cookies found in this request.

getHeader

public java.lang.String getHeader(java.lang.String name)
Returns the value of a header field, or null if not known. The case of the header field name is ignored.
Specified by:
getHeader in interface javax.servlet.http.HttpServletRequest
Parameters:
name - the case-insensitive header field name

getIntHeader

public int getIntHeader(java.lang.String name)
Returns the value of an integer header field, or -1 if not found. The case of the header field name is ignored.
Specified by:
getIntHeader in interface javax.servlet.http.HttpServletRequest
Parameters:
name - the case-insensitive header field name

getDateHeader

public long getDateHeader(java.lang.String name)
Returns the value of a date header field, or -1 if not found. The case of the header field name is ignored.
Specified by:
getDateHeader in interface javax.servlet.http.HttpServletRequest
Parameters:
name - the case-insensitive header field name

getHeaderNames

public java.util.Enumeration getHeaderNames()
Returns an enumeration of strings representing the header names for this request. Some server implementations do not allow headers to be accessed in this way, in which case this method will return null.
Specified by:
getHeaderNames in interface javax.servlet.http.HttpServletRequest

getHeaders

public java.util.Enumeration getHeaders(java.lang.String name)
Returns all the values of the specified request header as an Enumeration of String objects.
Specified by:
getHeaders in interface javax.servlet.http.HttpServletRequest

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Calls the corresponding method in ServletRequest-object.

getAttributeNames

public java.util.Enumeration getAttributeNames()
Calls the corresponding method in ServletRequest-object.

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Calls the corresponding method in ServletRequest-object.

getInputStream

public javax.servlet.ServletInputStream getInputStream()
                                                throws java.io.IOException
Calls the corresponding method in ServletRequest-object.

getLocale

public java.util.Locale getLocale()
Calls the corresponding method in ServletRequest-object.

getLocales

public java.util.Enumeration getLocales()
Calls the corresponding method in ServletRequest-object.

getReader

public java.io.BufferedReader getReader()
                                 throws java.io.IOException
Calls the corresponding method in ServletRequest-object.

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Calls the corresponding method in ServletRequest-object.

getRequestedSessionId

public java.lang.String getRequestedSessionId()
Specified by:
getRequestedSessionId in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#getRequestedSessionId

getPathInfo

public java.lang.String getPathInfo()
Specified by:
getPathInfo in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#getPathInfo

isRequestedSessionIdFromCookie

public boolean isRequestedSessionIdFromCookie()
Specified by:
isRequestedSessionIdFromCookie in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#isRequestedSessionIdFromCookie

isRequestedSessionIdFromUrl

public boolean isRequestedSessionIdFromUrl()
Specified by:
isRequestedSessionIdFromUrl in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#isRequestedSessionIdFromUrl

isRequestedSessionIdFromURL

public boolean isRequestedSessionIdFromURL()
Specified by:
isRequestedSessionIdFromURL in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#isRequestedSessionIdFromURL

getSession

public javax.servlet.http.HttpSession getSession()
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#getSession

getSession

public javax.servlet.http.HttpSession getSession(boolean create)
Specified by:
getSession in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#getSession

getUserPrincipal

public java.security.Principal getUserPrincipal()
Specified by:
getUserPrincipal in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#getUserPrinicpal

isRequestedSessionIdValid

public boolean isRequestedSessionIdValid()
Specified by:
isRequestedSessionIdValid in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#isRequestedSessionIdValid

isUserInRole

public boolean isUserInRole(java.lang.String role)
Specified by:
isUserInRole in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#isUserInRole

getServletPath

public java.lang.String getServletPath()
Specified by:
getServletPath in interface javax.servlet.http.HttpServletRequest
See Also:
HttpPresentationRequest#getServletPath