public class FileUpload
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
FileUpload.SectionHeaders
This class contains information extracted from the section headers for a form field
or file upload field.
|
private static class |
FileUpload.UploadStatus
This class contains information about the read status on the ServletInputStream
|
private class |
FileUpload.UploadStream |
Modifier and Type | Field and Description |
---|---|
private byte[] |
boundary
The multipart/formdata boundary
|
static long |
DEFAULT_TIMEOUT
The default timeout to use when waiting for the next data packet to
arrive.
|
private static java.lang.String |
EMPTY_STRING |
private static java.util.regex.Pattern |
FILENAME_PATTERN |
private java.util.Map<java.lang.String,java.util.List<UploadedFile>> |
files
Contains information about uploaded files
name --> UploadedFile
|
private ServletInputStream |
in
The stream coming from the browser.
|
private static org.slf4j.Logger |
log
Debug file upload.
|
private static java.util.regex.Pattern |
NAME_PATTERN |
private java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
parameters
Contains information about regular input field parameters
name --> value
|
private FileUploadProgress |
progress
Progress information for the upload
|
private FileUpload.UploadStatus |
status
The status of the upload.
|
private long |
timeout
The timeout in milliseconds that we wait for more data to arrive.
|
private int |
transferRate
The maximum transefer rate (bytes/second)
|
Constructor and Description |
---|
FileUpload(ServletRequest request)
Create a new
FileUpload object which will read the posted
data from the given ServletRequest object. |
FileUpload(ServletRequest request,
int transferRate,
long timeout)
Create a new
FileUpload object which will read the posted
data from the given ServletRequest object limiting the
transfer rate. |
Modifier and Type | Method and Description |
---|---|
private void |
addFile(java.lang.String name,
UploadedFile file)
Store information about an uploaded file.
|
private void |
addParameter(java.lang.String name,
java.lang.String value)
Store the value of a regular form field.
|
private void |
checkAbortAndDelay()
Check if upload has been aborted.
|
private void |
checkBoundary(byte[] line,
int lineLength,
byte[] boundary,
int boundaryLength)
Check if the given
line matches the boundary . |
(package private) void |
doUpload(java.io.OutputStream out,
UploadedFile uf)
Upload a single file section to the specified output stream
|
boolean |
eof()
Check if the end of the upload has been reached or not.
|
UploadedFile |
getFile(java.lang.String name)
Get the file information for an uplodaded file.
|
UploadedFile[] |
getFileValues(java.lang.String name)
Get the file information for uplodaded files.
|
private java.lang.String |
getHeaderAttribute(java.lang.String header,
java.util.regex.Pattern p) |
(package private) java.io.InputStream |
getInputStream(UploadedFile uf)
Get an input stream for reading from the current file section.
|
java.lang.String |
getParameter(java.lang.String name)
Get the value for a form field.
|
java.util.Iterator |
getParameterNames()
Get the names for all fields used in the form, excluding file input fields.
|
java.lang.String[] |
getParameterValues(java.lang.String name)
Get an array of values for a form field.
|
FileUploadProgress |
getProgress()
Get progress information about the upload.
|
UploadedFile |
next()
Read from the input stream until a file section is found or
the end is encountered.
|
private java.lang.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 java.lang.String |
readLineAsString()
Reads the next line of input and returns it as a string.
|
private FileUpload.SectionHeaders |
readSectionHeaders()
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 |
setException(java.lang.Throwable t)
Set exception information on the associated
FileUploadProgress
object. |
private static final org.slf4j.Logger log
public static final long DEFAULT_TIMEOUT
private static final java.lang.String EMPTY_STRING
private java.util.Map<java.lang.String,java.util.List<java.lang.String>> parameters
private java.util.Map<java.lang.String,java.util.List<UploadedFile>> files
private ServletInputStream in
private FileUpload.UploadStatus status
private FileUploadProgress progress
private byte[] boundary
private int transferRate
private long timeout
private static final java.util.regex.Pattern NAME_PATTERN
private static final java.util.regex.Pattern FILENAME_PATTERN
public FileUpload(ServletRequest request) throws java.io.IOException, UploadAbortedException
FileUpload
object which will read the posted
data from the given ServletRequest
object.request
- The ServletRequest
object containg information
about the request (and file upload)java.io.IOException
- If there is an error reading from the request streamUploadAbortedException
- If the upload was abortedpublic FileUpload(ServletRequest request, int transferRate, long timeout) throws java.io.IOException, UploadAbortedException
FileUpload
object which will read the posted
data from the given ServletRequest
object limiting the
transfer rate.request
- The ServletRequest
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 arrivejava.io.IOException
- If there is an error reading from the request streamUploadAbortedException
- If the upload was abortedpublic FileUploadProgress getProgress()
public boolean eof()
public UploadedFile next() throws java.io.IOException, UploadAbortedException
UploadedFile
object with information about the next file
section, or null if there are no more file sectionsjava.io.IOException
- If reading the file fails.UploadAbortedException
- If the uploaded has been requested to abort.void doUpload(java.io.OutputStream out, UploadedFile uf) throws java.io.IOException, UploadAbortedException
java.io.IOException
UploadAbortedException
java.io.InputStream getInputStream(UploadedFile uf) throws java.io.IOException
java.io.IOException
private void addParameter(java.lang.String name, java.lang.String value)
name
- The name of the form fieldvalue
- The valueprivate void addFile(java.lang.String name, UploadedFile file)
name
- The name of the form fieldfile
- A UploadedFile
object containing information
about the uploaded filepublic java.lang.String getParameter(java.lang.String name)
getParameterValues(String)
method.name
- The name of the form fieldgetParameterValues(String)
public java.lang.String[] getParameterValues(java.lang.String name)
getParameter(String)
method.name
- The name of the form fieldgetParameter(String)
public java.util.Iterator getParameterNames()
Iterator
object that can be used to iterate through
the collection of form fieldspublic UploadedFile getFile(java.lang.String name)
getFileValues(String)
method.name
- The name of the upload form fieldUploadedFile
object or NULL if it the field with this name is not foundgetFileValues(String)
public UploadedFile[] getFileValues(java.lang.String name)
getFile(String)
method.name
- The name of the upload form field(s)UploadedFile
objects or NULL if it no fields with
this name are not foundgetFile
public void setException(java.lang.Throwable t)
FileUploadProgress
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.t
- The throwable object to set.private java.lang.String getHeaderAttribute(java.lang.String header, java.util.regex.Pattern p)
private void checkBoundary(byte[] line, int lineLength, byte[] boundary, int boundaryLength)
line
matches the boundary
.
The line
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.private java.lang.String readLineAsString() throws java.io.IOException
java.io.IOException
private java.lang.String readFormData(byte[] boundary) throws java.io.IOException, UploadAbortedException
java.io.IOException
UploadAbortedException
private void readToBoundary(byte[] boundary) throws java.io.IOException, UploadAbortedException
java.io.IOException
UploadAbortedException
private FileUpload.SectionHeaders readSectionHeaders() throws java.io.IOException, UploadAbortedException
SectionHeaders
object with information about the headers foundjava.io.IOException
UploadAbortedException
private void checkAbortAndDelay() throws UploadAbortedException
UploadAbortedException
private int readLine(byte[] b, int off, int len) throws java.io.IOException
java.io.IOException