Class DiskFileItem
- java.lang.Object
-
- com.helger.web.fileupload.parse.DiskFileItem
-
- All Implemented Interfaces:
com.helger.commons.io.IHasInputStream,IFileItem,IFileItemHeadersSupport,jakarta.activation.DataSource
@NotThreadSafe public class DiskFileItem extends Object implements IFileItem, IFileItemHeadersSupport
The default implementation of the
IFileIteminterface.After retrieving an instance you may either request all contents of file at once using
directGet()or request anInputStreamwithgetInputStream()and process the file without attempting to load it into memory, which may come handy with large files.- Since:
- FileUpload 1.1
- Version:
- $Id: DiskFileItem.java 963609 2010-07-13 06:56:47Z jochen $
- Author:
- Rafal Krzewski, Sean Legassick, Jason van Zyl, John McNally, Martin Cooper, Sean C. Sullivan
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_CHARSETDefault content charset to be used when no explicit charset parameter is provided by the sender.static CharsetDEFAULT_CHARSET_OBJDefault content charset to be used when no explicit charset parameter is provided by the sender.
-
Constructor Summary
Constructors Constructor Description DiskFileItem(String sFieldName, String sContentType, boolean bIsFormField, String sFilename, int nSizeThreshold, File aRepository)Constructs a newDiskFileIteminstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddelete()Deletes the underlying storage for a file item, including deleting any associated temporary disk file.byte[]directGet()Returns the contents of the file as an array of bytes.StringgetCharSet()Returns the content charset passed by the agent ornullif not defined.StringgetContentType()byte[]getCopy()StringgetFieldName()Returns the name of the field in the multipart form corresponding to this file item.IFileItemHeadersgetHeaders()Returns the collection of headers defined locally within this item.InputStreamgetInputStream()StringgetName()Returns the original filename in the client's filesystem, as provided by the browser (or other client software).StringgetNameSecure()Returns the original filename in the client's filesystem, as provided by the browser (or other client software).StringgetNameUnchecked()Returns the original filename in the client's filesystem, as provided by the browser (or other client software).DeferredFileOutputStreamgetOutputStream()longgetSize()Returns the size of the file.FilegetStoreLocation()Returns theFileobject for theFileItem's data's temporary location on the disk.StringgetString()Returns the contents of the file item as a String, using the default character encoding (if one provided, it is used).StringgetStringWithFallback(Charset aFallbackCharset)Get the string with the charset defined in the content type.FilegetTempDirectory()protected FilegetTempFile()Creates and returns aFilerepresenting a uniquely named temporary file in the configured repository path.booleanisFormField()Determines whether or not aFileIteminstance represents a simple form field.booleanisInMemory()Provides a hint as to whether or not the file contents will be read from memory.booleanisReadMultiple()voidonEndOfRequest()Removes the file contents from the temporary storage.voidsetFieldName(String sFieldName)Sets the field name used to reference this file item.voidsetFormField(boolean bIsFormField)Specifies whether or not aFileIteminstance represents a simple form field.voidsetHeaders(IFileItemHeaders aHeaders)Sets the headers read from within an item.StringtoString()com.helger.commons.state.ISuccessIndicatorwrite(File aDstFile)A convenience method to write an uploaded item to disk.
-
-
-
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 newDiskFileIteminstance.- Parameters:
sFieldName- The name of the form field.sContentType- The content type passed by the browser ornullif 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, ornullif 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.nullmeans 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 aFilerepresenting a uniquely named temporary file in the configured repository path. The lifetime of the file is tied to the lifetime of theFileIteminstance; the file will be deleted when the instance is garbage collected.- Returns:
- The
Fileto be used for temporary storage.
-
getInputStream
@Nonnull public InputStream getInputStream()
- Specified by:
getInputStreamin interfacejakarta.activation.DataSource- Specified by:
getInputStreamin interfaceIFileItem- Specified by:
getInputStreamin interfacecom.helger.commons.io.IHasInputStream- Returns:
- An
InputStreamthat can be used to retrieve the contents of the file.
-
isReadMultiple
public boolean isReadMultiple()
- Specified by:
isReadMultiplein interfacecom.helger.commons.io.IHasInputStream
-
getContentType
@Nullable public String getContentType()
- Specified by:
getContentTypein interfacejakarta.activation.DataSource- Specified by:
getContentTypein interfaceIFileItem- Returns:
- The content type passed by the user agent or
nullif not defined.
-
getCharSet
@Nullable public String getCharSet()
Returns the content charset passed by the agent ornullif not defined.- Returns:
- The content charset passed by the agent or
nullif not defined.
-
getNameUnchecked
@Nullable public String getNameUnchecked()
Description copied from interface:IFileItemReturns 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:
getNameUncheckedin interfaceIFileItem- Returns:
- The original filename in the client's filesystem.
-
getName
@Nullable public String getName()
Description copied from interface:IFileItemReturns 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.
-
getNameSecure
@Nullable public String getNameSecure()
Description copied from interface:IFileItemReturns 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 toIFileItem.getName()this method automatically removes everything and including a NUL byte and therefore does not throw anInvalidFileNameException.- Specified by:
getNameSecurein interfaceIFileItem- 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:
isInMemoryin interfaceIFileItem- Returns:
trueif the file contents will be read from memory;falseotherwise.
-
getSize
@Nonnegative public long getSize()
Returns the size of the file.
-
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.
-
getCopy
@Nullable @ReturnsMutableCopy public byte[] getCopy()
-
getString
@Nonnull public String getString()
Description copied from interface:IFileItemReturns the contents of the file item as a String, using the default character encoding (if one provided, it is used). This method usesIFileItem.directGet()to retrieve the contents of the item.
-
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 benull.- 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:
writein interfaceIFileItem- Parameters:
aDstFile- TheFileinto 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 theFileIteminstance is garbage collected, this method can be used to ensure that this is done at an earlier time, thus preserving system resources.
-
getFieldName
@Nullable public String getFieldName()
Returns the name of the field in the multipart form corresponding to this file item.- Specified by:
getFieldNamein interfaceIFileItem- Returns:
- The name of the form field.
- See Also:
setFieldName(java.lang.String)
-
setFieldName
public void setFieldName(@Nullable String sFieldName)
Sets the field name used to reference this file item.- Specified by:
setFieldNamein interfaceIFileItem- Parameters:
sFieldName- The name of the form field.- See Also:
getFieldName()
-
isFormField
public boolean isFormField()
Determines whether or not aFileIteminstance represents a simple form field.- Specified by:
isFormFieldin interfaceIFileItem- Returns:
trueif the instance represents a simple form field;falseif it represents an uploaded file.- See Also:
setFormField(boolean)
-
setFormField
public void setFormField(boolean bIsFormField)
Specifies whether or not aFileIteminstance represents a simple form field.- Specified by:
setFormFieldin interfaceIFileItem- Parameters:
bIsFormField-trueif the instance represents a simple form field;falseif it represents an uploaded file.- See Also:
isFormField()
-
getHeaders
@Nullable public IFileItemHeaders getHeaders()
Description copied from interface:IFileItemHeadersSupportReturns the collection of headers defined locally within this item.- Specified by:
getHeadersin interfaceIFileItemHeadersSupport- Returns:
- the
IFileItemHeaderspresent for this item.
-
setHeaders
public void setHeaders(@Nullable IFileItemHeaders aHeaders)
Description copied from interface:IFileItemHeadersSupportSets the headers read from within an item. Implementations ofIFileItemorIFileItemStreamshould implement this interface to be able to get the raw headers found within the item header block.- Specified by:
setHeadersin interfaceIFileItemHeadersSupport- Parameters:
aHeaders- the instance that holds onto the headers for this instance.
-
getOutputStream
@Nonnull public DeferredFileOutputStream getOutputStream()
- Specified by:
getOutputStreamin interfacejakarta.activation.DataSource- Specified by:
getOutputStreamin interfaceIFileItem- Returns:
- An
OutputStreamthat can be used for storing the contents of the file.
-
getStoreLocation
@Nullable public File getStoreLocation()
Returns theFileobject for theFileItem's data's temporary location on the disk. Note that forFileItems that have their data stored in memory, this method will returnnull. When handling large files, you can useFile.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
nullif the data is stored in memory.
-
onEndOfRequest
public void onEndOfRequest()
Removes the file contents from the temporary storage. This was previously handled in afinalizemethod.- Specified by:
onEndOfRequestin interfaceIFileItem- Since:
- v10.0.0
-
-