Class DiskFileItem

    • Field Detail

      • DEFAULT_CHARSET_OBJ

        public static final Charset DEFAULT_CHARSET_OBJ
        Default content charset to be used when no explicit charset parameter is provided by the sender. Media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.
      • DEFAULT_CHARSET

        public static final String DEFAULT_CHARSET
        Default content charset to be used when no explicit charset parameter is provided by the sender. Media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.
    • Constructor Detail

      • DiskFileItem

        public DiskFileItem​(@Nullable
                            String sFieldName,
                            @Nullable
                            String sContentType,
                            boolean bIsFormField,
                            @Nullable
                            String sFilename,
                            @Nonnegative
                            int nSizeThreshold,
                            @Nullable
                            File aRepository)
        Constructs a new DiskFileItem instance.
        Parameters:
        sFieldName - The name of the form field.
        sContentType - The content type passed by the browser or null if not specified.
        bIsFormField - Whether or not this item is a plain form field, as opposed to a file upload.
        sFilename - The original filename in the user's file system, or null if not specified.
        nSizeThreshold - The threshold, in bytes, below which items will be retained in memory and above which they will be stored as a file.
        aRepository - The data repository, which is the directory in which files will be created, should the item size exceed the threshold. null means default temp directory.
    • Method Detail

      • getTempDirectory

        @Nonnull
        public final File getTempDirectory()
        Returns:
        The base directory for all temporary files.
      • getTempFile

        @Nonnull
        protected File getTempFile()
        Creates and returns a File representing a uniquely named temporary file in the configured repository path. The lifetime of the file is tied to the lifetime of the FileItem instance; the file will be deleted when the instance is garbage collected.
        Returns:
        The File to be used for temporary storage.
      • getInputStream

        @Nonnull
        public InputStream getInputStream()
        Specified by:
        getInputStream in interface jakarta.activation.DataSource
        Specified by:
        getInputStream in interface IFileItem
        Specified by:
        getInputStream in interface com.helger.commons.io.IHasInputStream
        Returns:
        An InputStream that can be used to retrieve the contents of the file.
      • isReadMultiple

        public boolean isReadMultiple()
        Specified by:
        isReadMultiple in interface com.helger.commons.io.IHasInputStream
      • getContentType

        @Nullable
        public String getContentType()
        Specified by:
        getContentType in interface jakarta.activation.DataSource
        Specified by:
        getContentType in interface IFileItem
        Returns:
        The content type passed by the user agent or null if not defined.
      • getCharSet

        @Nullable
        public String getCharSet()
        Returns the content charset passed by the agent or null if not defined.
        Returns:
        The content charset passed by the agent or null if not defined.
      • getNameUnchecked

        @Nullable
        public String getNameUnchecked()
        Description copied from interface: IFileItem
        Returns the original filename in the client's filesystem, as provided by the browser (or other client software). In most cases, this will be the base file name, without path information. However, some clients, such as the Opera browser, do include path information.
        Specified by:
        getNameUnchecked in interface IFileItem
        Returns:
        The original filename in the client's filesystem.
      • getName

        @Nullable
        public String getName()
        Description copied from interface: IFileItem
        Returns the original filename in the client's filesystem, as provided by the browser (or other client software). In most cases, this will be the base file name, without path information. However, some clients, such as the Opera browser, do include path information.
        Specified by:
        getName in interface jakarta.activation.DataSource
        Specified by:
        getName in interface IFileItem
        Returns:
        The original filename in the client's filesystem.
      • getNameSecure

        @Nullable
        public String getNameSecure()
        Description copied from interface: IFileItem
        Returns the original filename in the client's filesystem, as provided by the browser (or other client software). In most cases, this will be the base file name, without path information. However, some clients, such as the Opera browser, do include path information. Compared to IFileItem.getName() this method automatically removes everything and including a NUL byte and therefore does not throw an InvalidFileNameException.
        Specified by:
        getNameSecure in interface IFileItem
        Returns:
        The original filename in the client's filesystem without invalid characters.
      • isInMemory

        public boolean isInMemory()
        Provides a hint as to whether or not the file contents will be read from memory.
        Specified by:
        isInMemory in interface IFileItem
        Returns:
        true if the file contents will be read from memory; false otherwise.
      • getSize

        @Nonnegative
        public long getSize()
        Returns the size of the file.
        Specified by:
        getSize in interface IFileItem
        Returns:
        The size of the file, in bytes.
      • directGet

        @ReturnsMutableObject("Speed")
        @Nullable
        public byte[] directGet()
        Returns the contents of the file as an array of bytes. If the contents of the file were not yet cached in memory, they will be loaded from the disk storage and cached.
        Specified by:
        directGet in interface IFileItem
        Returns:
        The contents of the file as an array of bytes.
      • getCopy

        @Nullable
        @ReturnsMutableCopy
        public byte[] getCopy()
      • getString

        @Nonnull
        public String getString()
        Description copied from interface: IFileItem
        Returns the contents of the file item as a String, using the default character encoding (if one provided, it is used). This method uses IFileItem.directGet() to retrieve the contents of the item.
        Specified by:
        getString in interface IFileItem
        Returns:
        The contents of the item, as a string.
      • getStringWithFallback

        @Nonnull
        public String getStringWithFallback​(@Nonnull
                                            Charset aFallbackCharset)
        Get the string with the charset defined in the content type.
        Parameters:
        aFallbackCharset - The fallback charset to be used if the content type does not include a charset. May not be null.
        Returns:
        The string representation of the item.
      • write

        @Nonnull
        public com.helger.commons.state.ISuccessIndicator write​(@Nonnull
                                                                File aDstFile)
                                                         throws FileUploadException
        A convenience method to write an uploaded item to disk. The client code is not concerned with whether or not the item is stored in memory, or on disk in a temporary location. They just want to write the uploaded item to a file.

        This implementation first attempts to rename the uploaded item to the specified destination file, if the item was originally written to disk. Otherwise, the data will be copied to the specified file.

        This method is only guaranteed to work once, the first time it is invoked for a particular item. This is because, in the event that the method renames a temporary file, that file will no longer be available to copy or rename again at a later time.

        Specified by:
        write in interface IFileItem
        Parameters:
        aDstFile - The File into which the uploaded item should be stored.
        Returns:
        Never null
        Throws:
        FileUploadException - if an error occurs.
      • delete

        public void delete()
        Deletes the underlying storage for a file item, including deleting any associated temporary disk file. Although this storage will be deleted automatically when the FileItem instance is garbage collected, this method can be used to ensure that this is done at an earlier time, thus preserving system resources.
        Specified by:
        delete in interface IFileItem
      • setFieldName

        public void setFieldName​(@Nullable
                                 String sFieldName)
        Sets the field name used to reference this file item.
        Specified by:
        setFieldName in interface IFileItem
        Parameters:
        sFieldName - The name of the form field.
        See Also:
        getFieldName()
      • isFormField

        public boolean isFormField()
        Determines whether or not a FileItem instance represents a simple form field.
        Specified by:
        isFormField in interface IFileItem
        Returns:
        true if the instance represents a simple form field; false if it represents an uploaded file.
        See Also:
        setFormField(boolean)
      • setFormField

        public void setFormField​(boolean bIsFormField)
        Specifies whether or not a FileItem instance represents a simple form field.
        Specified by:
        setFormField in interface IFileItem
        Parameters:
        bIsFormField - true if the instance represents a simple form field; false if it represents an uploaded file.
        See Also:
        isFormField()
      • getStoreLocation

        @Nullable
        public File getStoreLocation()
        Returns the File object for the FileItem's data's temporary location on the disk. Note that for FileItems that have their data stored in memory, this method will return null. When handling large files, you can use File.renameTo(java.io.File) to move the file to new location without copying the data, if the source and destination locations reside within the same logical volume.
        Returns:
        The data file, or null if the data is stored in memory.
      • onEndOfRequest

        public void onEndOfRequest()
        Removes the file contents from the temporary storage. This was previously handled in a finalize method.
        Specified by:
        onEndOfRequest in interface IFileItem
        Since:
        v10.0.0