@NotThreadSafe public class DiskFileItem extends Object implements IFileItem, IFileItemHeadersSupport
The default implementation of the IFileItem interface.
After retrieving an instance you may either request all contents of file at
once using directGet() or request an InputStream with getInputStream() and process the file without
attempting to load it into memory, which may come handy with large files.
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_CHARSET
Default content charset to be used when no explicit charset parameter is
provided by the sender.
|
static Charset |
DEFAULT_CHARSET_OBJ
Default content charset to be used when no explicit charset parameter is
provided by the sender.
|
| Constructor and Description |
|---|
DiskFileItem(String sFieldName,
String sContentType,
boolean bIsFormField,
String sFilename,
int nSizeThreshold,
File aRepository)
Constructs a new
DiskFileItem instance. |
| Modifier and Type | Method and Description |
|---|---|
void |
delete()
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.
|
protected void |
finalize()
Removes the file contents from the temporary storage.
|
String |
getCharSet()
Returns the content charset passed by the agent or
null if not
defined. |
String |
getContentType() |
byte[] |
getCopy() |
String |
getFieldName()
Returns the name of the field in the multipart form corresponding to this
file item.
|
IFileItemHeaders |
getHeaders()
Returns the collection of headers defined locally within this item.
|
InputStream |
getInputStream() |
String |
getName()
Returns the original filename in the client's filesystem, as provided by
the browser (or other client software).
|
String |
getNameSecure()
Returns the original filename in the client's filesystem, as provided by
the browser (or other client software).
|
String |
getNameUnchecked()
Returns the original filename in the client's filesystem, as provided by
the browser (or other client software).
|
DeferredFileOutputStream |
getOutputStream() |
long |
getSize()
Returns the size of the file.
|
File |
getStoreLocation()
Returns the
File object for the FileItem's
data's temporary location on the disk. |
String |
getString()
Returns the contents of the file item as a String, using the default
character encoding (if one provided, it is used).
|
String |
getStringWithFallback(Charset aFallbackCharset)
Get the string with the charset defined in the content type.
|
File |
getTempDirectory() |
protected File |
getTempFile()
Creates and returns a
File representing a uniquely named temporary
file in the configured repository path. |
boolean |
isFormField()
Determines whether or not a
FileItem instance represents a
simple form field. |
boolean |
isInMemory()
Provides a hint as to whether or not the file contents will be read from
memory.
|
boolean |
isReadMultiple() |
void |
setFieldName(String sFieldName)
Sets the field name used to reference this file item.
|
void |
setFormField(boolean bIsFormField)
Specifies whether or not a
FileItem instance represents a
simple form field. |
void |
setHeaders(IFileItemHeaders aHeaders)
Sets the headers read from within an item.
|
String |
toString() |
com.helger.commons.state.ISuccessIndicator |
write(File aDstFile)
A convenience method to write an uploaded item to disk.
|
public static final String DEFAULT_CHARSET
public static final Charset DEFAULT_CHARSET_OBJ
public DiskFileItem(@Nullable String sFieldName, @Nullable String sContentType, boolean bIsFormField, @Nullable String sFilename, @Nonnegative int nSizeThreshold, @Nullable File aRepository)
DiskFileItem instance.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.@Nonnull public final File getTempDirectory()
@Nonnull protected File getTempFile()
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.File to be used for temporary storage.@Nonnull public InputStream getInputStream()
getInputStream in interface com.helger.commons.io.IHasInputStreamgetInputStream in interface IFileItemgetInputStream in interface DataSourceInputStream that can be used to retrieve the contents of
the file.public boolean isReadMultiple()
isReadMultiple in interface com.helger.commons.io.IHasInputStream@Nullable public String getContentType()
getContentType in interface IFileItemgetContentType in interface DataSourcenull if
not defined.@Nullable public String getCharSet()
null if not
defined.null if not
defined.@Nullable public String getNameUnchecked()
IFileItemgetNameUnchecked in interface IFileItem@Nullable public String getName()
IFileItemgetName in interface IFileItemgetName in interface DataSource@Nullable public String getNameSecure()
IFileItemIFileItem.getName() this method automatically removes everything and
including a NUL byte and therefore does not throw an
InvalidFileNameException.getNameSecure in interface IFileItempublic boolean isInMemory()
isInMemory in interface IFileItemtrue if the file contents will be read from memory;
false otherwise.@Nonnegative public long getSize()
@ReturnsMutableObject(value="Speed") @Nullable public byte[] directGet()
@Nullable @ReturnsMutableCopy public byte[] getCopy()
@Nonnull public String getString()
IFileItemIFileItem.directGet() to retrieve the contents of the item.@Nonnull public String getStringWithFallback(@Nonnull Charset aFallbackCharset)
aFallbackCharset - The fallback charset to be used if the content type does not include
a charset. May not be null.@Nonnull public com.helger.commons.state.ISuccessIndicator write(@Nonnull File aDstFile) throws FileUploadException
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.
write in interface IFileItemaDstFile - The File into which the uploaded item should be stored.FileUploadException - if an error occurs.public void delete()
FileItem instance is garbage collected,
this method can be used to ensure that this is done at an earlier time,
thus preserving system resources.@Nullable public String getFieldName()
getFieldName in interface IFileItemsetFieldName(java.lang.String)public void setFieldName(@Nullable String sFieldName)
setFieldName in interface IFileItemsFieldName - The name of the form field.getFieldName()public boolean isFormField()
FileItem instance represents a
simple form field.isFormField in interface IFileItemtrue if the instance represents a simple form field;
false if it represents an uploaded file.setFormField(boolean)public void setFormField(boolean bIsFormField)
FileItem instance represents a
simple form field.setFormField in interface IFileItembIsFormField - true if the instance represents a simple form field;
false if it represents an uploaded file.isFormField()@Nullable public IFileItemHeaders getHeaders()
IFileItemHeadersSupportgetHeaders in interface IFileItemHeadersSupportIFileItemHeaders present for this item.public void setHeaders(@Nullable IFileItemHeaders aHeaders)
IFileItemHeadersSupportIFileItem or IFileItemStream should implement this
interface to be able to get the raw headers found within the item header
block.setHeaders in interface IFileItemHeadersSupportaHeaders - the instance that holds onto the headers for this instance.@Nonnull public DeferredFileOutputStream getOutputStream()
getOutputStream in interface IFileItemgetOutputStream in interface DataSourceOutputStream that can be used for storing the contents
of the file.@Nullable public File getStoreLocation()
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.null if the data is stored in
memory.protected void finalize()
throws Throwable
Copyright © 2014–2019 Philip Helger. All rights reserved.