Fields Summary |
---|
private static int | counterCounter used in unique identifier generation. |
private String | fieldNameThe name of the form field as provided by the browser. |
private String | contentTypeThe content type passed by the browser, or null if
not defined. |
private boolean | isFormFieldWhether or not this item is a simple form field. |
private String | fileNameThe original filename in the user's filesystem. |
private int | sizeThresholdThe threshold above which uploads will be stored on disk. |
private File | repositoryThe directory in which uploaded files will be stored, if stored on disk. |
private byte[] | cachedContentCached contents of the file. |
private DeferredFileOutputStream | dfosOutput stream for this item. |
Methods Summary |
---|
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.
cachedContent = null;
File outputFile = getStoreLocation();
if (outputFile != null && outputFile.exists())
{
outputFile.delete();
}
|
protected void | finalize()Removes the file contents from the temporary storage.
File outputFile = dfos.getFile();
if (outputFile != null && outputFile.exists())
{
outputFile.delete();
}
|
public byte[] | get()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.
if (dfos.isInMemory())
{
if (cachedContent == null)
{
cachedContent = dfos.getData();
}
return cachedContent;
}
byte[] fileData = new byte[(int) getSize()];
FileInputStream fis = null;
try
{
fis = new FileInputStream(dfos.getFile());
fis.read(fileData);
}
catch (IOException e)
{
fileData = null;
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
// ignore
}
}
}
return fileData;
|
public java.lang.String | getContentType()Returns the content type passed by the browser or null if
not defined.
return contentType;
|
public java.lang.String | getFieldName()Returns the name of the field in the multipart form corresponding to
this file item.
return fieldName;
|
public java.io.InputStream | getInputStream()Returns an {@link java.io.InputStream InputStream} that can be
used to retrieve the contents of the file.
if (!dfos.isInMemory())
{
return new FileInputStream(dfos.getFile());
}
if (cachedContent == null)
{
cachedContent = dfos.getData();
}
return new ByteArrayInputStream(cachedContent);
|
public java.lang.String | getName()Returns the original filename in the client's filesystem.
return fileName;
|
public java.io.OutputStream | getOutputStream()Returns an {@link java.io.OutputStream OutputStream} that can
be used for storing the contents of the file.
if (dfos == null)
{
File outputFile = getTempFile();
dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
}
return dfos;
|
public long | getSize()Returns the size of the file.
if (cachedContent != null)
{
return cachedContent.length;
}
else if (dfos.isInMemory())
{
return dfos.getData().length;
}
else
{
return dfos.getFile().length();
}
|
public java.io.File | getStoreLocation()Returns the {@link java.io.File} object for the FileItem 's
data's temporary location on the disk. Note that for
FileItem s that have their data stored in memory,
this method will return null . When handling large
files, you can use {@link java.io.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.
return dfos.getFile();
|
public java.lang.String | getString(java.lang.String encoding)Returns the contents of the file as a String, using the specified
encoding. This method uses {@link #get()} to retrieve the
contents of the file.
return new String(get(), encoding);
|
public java.lang.String | getString()Returns the contents of the file as a String, using the default
character encoding. This method uses {@link #get()} to retrieve the
contents of the file.
return new String(get());
|
protected java.io.File | getTempFile()Creates and returns a {@link java.io.File File} representing a uniquely
named temporary file in the configured repository path.
File tempDir = repository;
if (tempDir == null)
{
tempDir = new File(System.getProperty("java.io.tmpdir"));
}
String fileName = "upload_" + getUniqueId() + ".tmp";
File f = new File(tempDir, fileName);
f.deleteOnExit();
return f;
|
private static java.lang.String | getUniqueId()Returns an identifier that is unique within the class loader used to
load this class, but does not have random-like apearance.
int current;
synchronized (DefaultFileItem.class)
{
current = counter++;
}
String id = Integer.toString(current);
// If you manage to get more than 100 million of ids, you'll
// start getting ids longer than 8 characters.
if (current < 100000000)
{
id = ("00000000" + id).substring(id.length());
}
return id;
|
public boolean | isFormField()Determines whether or not a FileItem instance represents
a simple form field.
return isFormField;
|
public boolean | isInMemory()Provides a hint as to whether or not the file contents will be read
from memory.
return (dfos.isInMemory());
|
public void | setFieldName(java.lang.String fieldName)Sets the field name used to reference this file item.
this.fieldName = fieldName;
|
public void | setFormField(boolean state)Specifies whether or not a FileItem instance represents
a simple form field.
isFormField = state;
|
public void | write(java.io.File file)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.
if (isInMemory())
{
FileOutputStream fout = null;
try
{
fout = new FileOutputStream(file);
fout.write(get());
}
finally
{
if (fout != null)
{
fout.close();
}
}
}
else
{
File outputFile = getStoreLocation();
if (outputFile != null)
{
/*
* The uploaded file is being stored on disk
* in a temporary location so move it to the
* desired file.
*/
if (!outputFile.renameTo(file))
{
BufferedInputStream in = null;
BufferedOutputStream out = null;
try
{
in = new BufferedInputStream(
new FileInputStream(outputFile));
out = new BufferedOutputStream(
new FileOutputStream(file));
byte[] bytes = new byte[2048];
int s = 0;
while ((s = in.read(bytes)) != -1)
{
out.write(bytes, 0, s);
}
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
// ignore
}
try
{
out.close();
}
catch (IOException e)
{
// ignore
}
}
}
}
else
{
/*
* For whatever reason we cannot write the
* file to disk.
*/
throw new FileUploadException(
"Cannot write uploaded file to disk!");
}
}
|