de.opus5.servlet
Class MultipartRequest

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

public class MultipartRequest
extends java.lang.Object
implements javax.servlet.ServletRequest

A class for handling multipart form uploads. Unlike com.oreilly.servlet.MultipartRequest this class returns a InputStream for reading the file's content, so you can also write the data into magic crystals instead of writing it to the file system. ;) There is also the option to save files to disk and return a java.io.File object. You can also access the file data via the getUploadedFile() method, which returns an file object implementing the de.opus5.servlet.UploadedFile interface. The class can decide based on the total request size if the file data is temporary stored in memory or in the file system.

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); 
 ....
 ....
 


Inner Class Summary
 class MultipartRequest.BucketHash
          Simple class for storing parameters
 
Constructor Summary
MultipartRequest(javax.servlet.ServletRequest sR, int maxMemStoredReqSiz, int maxReqSiz, java.lang.String tmpDir, java.lang.String uniquePrefix)
          Create a MultipartRequest, temporary files will be deleted.
MultipartRequest(javax.servlet.ServletRequest 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.ServletRequest 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.ServletRequest sR, java.lang.String tmpDir, java.lang.String uniquePrefix)
          Create a MultipartRequest using the ServletRequest object, temporary files will be deleted.
 
Method Summary
 java.lang.Object getAttribute(java.lang.String name)
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getCharacterEncoding()
          Calls the corresponding method in ServletRequest-object.
 int getContentLength()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getContentType()
          Calls the corresponding method in ServletRequest-object.
 java.io.File getFile(java.lang.String fieldname)
          returns a java.io.File object if the file was saved on disk, null otherwise.
 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.
 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 getProtocol()
          Calls the corresponding method in ServletRequest-object.
 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()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getRemoteHost()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getScheme()
          Calls the corresponding method in ServletRequest-object.
 java.lang.String getServerName()
          Calls the corresponding method in ServletRequest-object.
 int getServerPort()
          Calls the corresponding method in ServletRequest-object.
 UploadedFile getUploadedFile(java.lang.String fieldname)
          Get the UploadedFile object for the uploaded file with given name.
 
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.ServletRequest 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 ServletRequest
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.ServletRequest 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 ServletRequest
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.ServletRequest 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 ServletRequest
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.ServletRequest sR,
                        java.lang.String tmpDir,
                        java.lang.String uniquePrefix)
                 throws java.lang.IllegalArgumentException,
                        java.io.IOException
Create a MultipartRequest using the ServletRequest object, temporary files will be deleted. Maximum request size is 1MB, files will be saved to disk.
Parameters:
sR - the ServletRequest
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
Specified by:
getParameterNames in interface javax.servlet.ServletRequest
Returns:
Enumeration of parameter names

getParameter

public java.lang.String getParameter(java.lang.String name)
return the value of parameter with specified name
Specified by:
getParameter in interface javax.servlet.ServletRequest
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.
Specified by:
getParameterValues in interface javax.servlet.ServletRequest

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

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Calls the corresponding method in ServletRequest-object.
Specified by:
getAttribute in interface javax.servlet.ServletRequest

getContentLength

public int getContentLength()
Calls the corresponding method in ServletRequest-object.
Specified by:
getContentLength in interface javax.servlet.ServletRequest

getContentType

public java.lang.String getContentType()
Calls the corresponding method in ServletRequest-object.
Specified by:
getContentType in interface javax.servlet.ServletRequest

getProtocol

public java.lang.String getProtocol()
Calls the corresponding method in ServletRequest-object.
Specified by:
getProtocol in interface javax.servlet.ServletRequest

getScheme

public java.lang.String getScheme()
Calls the corresponding method in ServletRequest-object.
Specified by:
getScheme in interface javax.servlet.ServletRequest

getServerName

public java.lang.String getServerName()
Calls the corresponding method in ServletRequest-object.
Specified by:
getServerName in interface javax.servlet.ServletRequest

getServerPort

public int getServerPort()
Calls the corresponding method in ServletRequest-object.
Specified by:
getServerPort in interface javax.servlet.ServletRequest

getRemoteAddr

public java.lang.String getRemoteAddr()
Calls the corresponding method in ServletRequest-object.
Specified by:
getRemoteAddr in interface javax.servlet.ServletRequest

getRemoteHost

public java.lang.String getRemoteHost()
Calls the corresponding method in ServletRequest-object.
Specified by:
getRemoteHost in interface javax.servlet.ServletRequest

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Calls the corresponding method in ServletRequest-object.
Specified by:
getRealPath in interface javax.servlet.ServletRequest

getInputStream

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

getReader

public java.io.BufferedReader getReader()
                                 throws java.io.IOException
Calls the corresponding method in ServletRequest-object.
Specified by:
getReader in interface javax.servlet.ServletRequest

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Calls the corresponding method in ServletRequest-object.
Specified by:
getCharacterEncoding in interface javax.servlet.ServletRequest