Class FileUpload
java.lang.Object
net.sf.basedb.clients.web.fileupload.FileUpload
Objects of this class are used to handle file upload from browsers using
the standard mechanism described in RFC1867.
- Version:
- 2.0
- Author:
- Nicklas
- Last modified
- $Date$
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static class
This class contains information extracted from the section headers for a form field or file upload field.private static class
This class contains information about the read status on the ServletInputStreamprivate class
-
Field Summary
Modifier and TypeFieldDescriptionprivate byte[]
The multipart/formdata boundaryprivate Charset
The charset used for decoding header lines.static final long
The default timeout to use when waiting for the next data packet to arrive.private static final String
private static final Pattern
private Map<String,
List<UploadedFile>> Contains information about uploaded files name --> UploadedFileprivate ServletInputStream
The stream coming from the browser.private static final Logger
Debug file upload.private static final Pattern
Contains information about regular input field parameters name --> valueprivate FileUploadProgress
Progress information for the uploadprivate FileUpload.UploadStatus
The status of the upload.private long
The timeout in milliseconds that we wait for more data to arrive.private int
The maximum transefer rate (bytes/second) -
Constructor Summary
ConstructorDescriptionFileUpload
(ServletRequest request) Create a newFileUpload
object which will read the posted data from the givenServletRequest
object.FileUpload
(ServletRequest request, int transferRate, long timeout) Create a newFileUpload
object which will read the posted data from the givenServletRequest
object limiting the transfer rate. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addFile
(String name, UploadedFile file) Store information about an uploaded file.private void
addParameter
(String name, String value) Store the value of a regular form field.private void
Check if upload has been aborted.private void
checkBoundary
(byte[] line, int lineLength, byte[] boundary, int boundaryLength) Check if the givenline
matches theboundary
.(package private) void
doUpload
(OutputStream out, UploadedFile uf) Upload a single file section to the specified output streamboolean
eof()
Check if the end of the upload has been reached or not.Get the file information for an uplodaded file.getFileValues
(String name) Get the file information for uplodaded files.private String
getHeaderAttribute
(String header, Pattern p) (package private) InputStream
Get an input stream for reading from the current file section.getParameter
(String name) Get the value for a form field.Get the names for all fields used in the form, excluding file input fields.String[]
getParameterValues
(String name) Get an array of values for a form field.Get progress information about the upload.next()
Read from the input stream until a file section is found or the end is encountered.private String
readFormData
(byte[] boundary) Read from ServletInputStream until the boundary is found.private int
readLine
(byte[] b, int off, int len) Read next line of data into a buffer.private String
Reads the next line of input and returns it as a string.private FileUpload.SectionHeaders
Read from ServletInputStream until an empty line is found.private void
readToBoundary
(byte[] boundary) Read from ServletInputStream until the boundary is found on a line by itself.void
Set exception information on the associatedFileUploadProgress
object.
-
Field Details
-
log
Debug file upload. -
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUTThe default timeout to use when waiting for the next data packet to arrive.- See Also:
-
EMPTY_STRING
- See Also:
-
parameters
Contains information about regular input field parameters name --> value -
files
Contains information about uploaded files name --> UploadedFile -
in
The stream coming from the browser. -
status
The status of the upload. -
progress
Progress information for the upload -
boundary
private byte[] boundaryThe multipart/formdata boundary -
transferRate
private int transferRateThe maximum transefer rate (bytes/second) -
charset
The charset used for decoding header lines. -
timeout
private long timeoutThe timeout in milliseconds that we wait for more data to arrive. Default value is 60000 = 60 seconds. -
NAME_PATTERN
-
FILENAME_PATTERN
-
-
Constructor Details
-
FileUpload
Create a newFileUpload
object which will read the posted data from the givenServletRequest
object.- Parameters:
request
- TheServletRequest
object containg information about the request (and file upload)- Throws:
IOException
- If there is an error reading from the request streamUploadAbortedException
- If the upload was aborted
-
FileUpload
public FileUpload(ServletRequest request, int transferRate, long timeout) throws IOException, UploadAbortedException Create a newFileUpload
object which will read the posted data from the givenServletRequest
object limiting the transfer rate.- Parameters:
request
- TheServletRequest
object containg information about the request (and file upload)transferRate
- The maximum transfer rate in bytes per second, or 0 to transfer as fast as possibletimeout
- The timeout in milliseconds to wait for the next packet of data to arrive- Throws:
IOException
- If there is an error reading from the request streamUploadAbortedException
- If the upload was aborted
-
-
Method Details
-
getProgress
Get progress information about the upload. -
eof
public boolean eof()Check if the end of the upload has been reached or not.- Returns:
- TRUE if the end has been reached, FALSE otherwise
-
next
Read from the input stream until a file section is found or the end is encountered.- Returns:
- An
UploadedFile
object with information about the next file section, or null if there are no more file sections - Throws:
IOException
- If reading the file fails.UploadAbortedException
- If the uploaded has been requested to abort.
-
doUpload
Upload a single file section to the specified output stream- Throws:
IOException
UploadAbortedException
-
getInputStream
Get an input stream for reading from the current file section.- Throws:
IOException
-
addParameter
Store the value of a regular form field.- Parameters:
name
- The name of the form fieldvalue
- The value
-
addFile
Store information about an uploaded file.- Parameters:
name
- The name of the form fieldfile
- AUploadedFile
object containing information about the uploaded file
-
getParameter
Get the value for a form field. Use this method if the form contains only a single element with this name, otherwise use thegetParameterValues(String)
method.- Parameters:
name
- The name of the form field- Returns:
- The value of form field or NULL if it is not found
- See Also:
-
getParameterValues
Get an array of values for a form field. Use this method if the form contains several elements with the same name, otherwise use thegetParameter(String)
method.- Parameters:
name
- The name of the form field- Returns:
- An array of values, or NULL if no values are found
- See Also:
-
getParameterNames
Get the names for all fields used in the form, excluding file input fields.- Returns:
- An
Iterator
object that can be used to iterate through the collection of form fields
-
getFile
Get the file information for an uplodaded file. Use this method if the form contains only a single element with this name, otherwise use thegetFileValues(String)
method.- Parameters:
name
- The name of the upload form field- Returns:
- An
UploadedFile
object or NULL if it the field with this name is not found - See Also:
-
getFileValues
Get the file information for uplodaded files. Use this method if the form contains several elements with the same name, otherwise use thegetFile(String)
method.- Parameters:
name
- The name of the upload form field(s)- Returns:
- An array of
UploadedFile
objects or NULL if it no fields with this name are not found - See Also:
-
setException
Set exception information on the associatedFileUploadProgress
object. This is a useful way to communicate if the uploading is done by one thread and the progress display in another. This method is also called if an exception occures internally in this object.- Parameters:
t
- The throwable object to set.
-
getHeaderAttribute
-
checkBoundary
private void checkBoundary(byte[] line, int lineLength, byte[] boundary, int boundaryLength) Check if the givenline
matches theboundary
. Theline
always has an extra CRLF pair at the end and may also have two dashes (--). The two dashes indicates the end of the input for the form. -
readLineAsString
Reads the next line of input and returns it as a string. We assume that the length of the line is less than 1000 bytes, or this method will clip the string after that many characters. This method is used when reading section headers.- Returns:
- The string
- Throws:
IOException
-
readFormData
Read from ServletInputStream until the boundary is found. This method is used to read data from ordinary (not file upload) form fields.- Returns:
- A string representation of the data that was read
- Throws:
IOException
UploadAbortedException
-
readToBoundary
Read from ServletInputStream until the boundary is found on a line by itself. This method is used to skip data whenver necessary.- Throws:
IOException
UploadAbortedException
-
readSectionHeaders
Read from ServletInputStream until an empty line is found. This method is used to read section headers. return ASectionHeaders
object with information about the headers found- Throws:
IOException
UploadAbortedException
-
checkAbortAndDelay
Check if upload has been aborted. Check the transfer rate and wait if necessary.- Throws:
UploadAbortedException
-
readLine
Read next line of data into a buffer.- Returns:
- The number of bytes read or -1 if the end is reached
- Throws:
IOException
- Since:
- 2.1.2
-