Class FileUpload


  • public class FileUpload
    extends Object
    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$
    • Field Detail

      • log

        private static final org.slf4j.Logger log
        Debug file upload.
      • DEFAULT_TIMEOUT

        public static final long DEFAULT_TIMEOUT
        The default timeout to use when waiting for the next data packet to arrive.
        See Also:
        Constant Field Values
      • parameters

        private Map<String,​List<String>> parameters
        Contains information about regular input field parameters name --> value
      • boundary

        private byte[] boundary
        The multipart/formdata boundary
      • transferRate

        private int transferRate
        The maximum transefer rate (bytes/second)
      • charset

        private Charset charset
        The charset used for decoding header lines.
      • timeout

        private long timeout
        The timeout in milliseconds that we wait for more data to arrive. Default value is 60000 = 60 seconds.
      • NAME_PATTERN

        private static final Pattern NAME_PATTERN
      • FILENAME_PATTERN

        private static final Pattern FILENAME_PATTERN
    • Constructor Detail

      • FileUpload

        public FileUpload​(ServletRequest request)
                   throws IOException,
                          UploadAbortedException
        Create a new FileUpload object which will read the posted data from the given ServletRequest object.
        Parameters:
        request - The ServletRequest object containg information about the request (and file upload)
        Throws:
        IOException - If there is an error reading from the request stream
        UploadAbortedException - If the upload was aborted
      • FileUpload

        public FileUpload​(ServletRequest request,
                          int transferRate,
                          long timeout)
                   throws IOException,
                          UploadAbortedException
        Create a new FileUpload object which will read the posted data from the given ServletRequest object limiting the transfer rate.
        Parameters:
        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 possible
        timeout - 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 stream
        UploadAbortedException - If the upload was aborted
    • Method Detail

      • getProgress

        public FileUploadProgress 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
      • addParameter

        private void addParameter​(String name,
                                  String value)
        Store the value of a regular form field.
        Parameters:
        name - The name of the form field
        value - The value
      • addFile

        private void addFile​(String name,
                             UploadedFile file)
        Store information about an uploaded file.
        Parameters:
        name - The name of the form field
        file - A UploadedFile object containing information about the uploaded file
      • getParameter

        public String getParameter​(String name)
        Get the value for a form field. Use this method if the form contains only a single element with this name, otherwise use the getParameterValues(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(String)
      • getParameterValues

        public String[] getParameterValues​(String name)
        Get an array of values for a form field. Use this method if the form contains several elements with the same name, otherwise use the getParameter(String) method.
        Parameters:
        name - The name of the form field
        Returns:
        An array of values, or NULL if no values are found
        See Also:
        getParameter(String)
      • getParameterNames

        public Iterator<String> 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

        public UploadedFile getFile​(String name)
        Get the file information for an uplodaded file. Use this method if the form contains only a single element with this name, otherwise use the getFileValues(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(String)
      • getFileValues

        public UploadedFile[] getFileValues​(String name)
        Get the file information for uplodaded files. Use this method if the form contains several elements with the same name, otherwise use the getFile(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:
        getFile
      • setException

        public void setException​(Throwable t)
        Set exception information on the associated 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.
        Parameters:
        t - The throwable object to set.
      • checkBoundary

        private void checkBoundary​(byte[] line,
                                   int lineLength,
                                   byte[] boundary,
                                   int boundaryLength)
        Check if the given 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.
      • readLineAsString

        private String readLineAsString()
                                 throws IOException
        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
      • readLine

        private int readLine​(byte[] b,
                             int off,
                             int len)
                      throws IOException
        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