public class FileBean
extends java.lang.Object
Represents a file that was submitted as part of an HTTP POST request. Provides methods for examining information about the file, and the retrieving the contents of the file. When a file is uploaded by a user it is stored as a temporary file on the file system, which is wrapped by an instance of this class. This is necessary because browsers may send file upload segments before sending any other form parameters needed to identify what to do with the uploaded files!
The application developer is responsible for removing this temporary file once they have processed it. This can be accomplished in one of two ways. Firstly a call to save(File) will effect a save by moving the temporary file to the desired location. In this case there is no need to call delete(), although doing so will not delete the saved file. The second way is to simply call delete(). This is more applicable when consuming the file as an InputStream. An example code fragment for reading a text based file might look like this:
FileBean bean = getUserIcon(); BufferedReader reader = new BufferedReader( new InputStreamReader(bean.getInputStream()) ); String line = null while ( (line = reader.readLine()) != null) { // do something with line } bean.delete();
Constructor and Description |
---|
FileBean(java.io.File file,
java.lang.String contentType,
java.lang.String originalName)
Constructs a FileBean pointing to an on-disk representation of the file uploaded by the user.
|
FileBean(java.io.File file,
java.lang.String contentType,
java.lang.String originalName,
java.lang.String charset)
Constructs a FileBean pointing to an on-disk representation of the file uploaded by the user.
|
Modifier and Type | Method and Description |
---|---|
void |
delete()
Deletes the temporary file associated with this file upload if one still exists.
|
java.lang.String |
getContentType()
Returns the content type of the file that the user selected and uploaded.
|
java.lang.String |
getFileName()
Returns the name of the file that the user selected and uploaded (this is not necessarily
the name that the underlying file is now stored on the server using).
|
java.io.InputStream |
getInputStream()
Gets an input stream to read from the file uploaded
|
java.io.Reader |
getReader()
Gets a reader to read characters from the uploaded file.
|
java.io.Reader |
getReader(java.lang.String charset)
Gets a reader to read characters from the uploaded file using the given charset.
|
long |
getSize()
Gets the size of the file that was uploaded.
|
void |
save(java.io.File toFile)
Saves the uploaded file to the location on disk represented by File.
|
protected void |
saveViaCopy(java.io.File toFile)
Attempts to save the uploaded file to the specified file by performing a stream
based copy.
|
java.lang.String |
toString()
Returns the name of the file and the content type in a String format.
|
public FileBean(java.io.File file, java.lang.String contentType, java.lang.String originalName)
file
- the File object on the server which holds the uploaded contents of the filecontentType
- the content type of the file declared by the browser during uploadoriginalName
- the name of the file as declared by the user's browserpublic FileBean(java.io.File file, java.lang.String contentType, java.lang.String originalName, java.lang.String charset)
file
- the File object on the server which holds the uploaded contents of the filecontentType
- the content type of the file declared by the browser during uploadoriginalName
- the name of the file as declared by the user's browsercharset
- the charset specified by the servlet requestpublic java.lang.String getFileName()
public java.lang.String getContentType()
public long getSize()
public java.io.InputStream getInputStream() throws java.io.IOException
java.io.IOException
public java.io.Reader getReader() throws java.io.UnsupportedEncodingException, java.io.IOException
java.io.UnsupportedEncodingException
java.io.IOException
public java.io.Reader getReader(java.lang.String charset) throws java.io.UnsupportedEncodingException, java.io.IOException
charset
- the charset the reader should usejava.io.UnsupportedEncodingException
java.io.IOException
public void save(java.io.File toFile) throws java.io.IOException
toFile
- a File object representing a locationjava.io.IOException
- if the save will fail for a reason that we can detect up front, for
example, missing files, permissions etc. or we try to save get a failure.protected void saveViaCopy(java.io.File toFile) throws java.io.IOException
toFile
- the file to save tojava.io.IOException
public void delete() throws java.io.IOException
java.io.IOException
- if the delete will fail for a reason we can detect up front, or if
we try to delete and get a failurepublic java.lang.String toString()
toString
in class java.lang.Object
? Copyright 2005-2006, Stripes Development Team.