|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.action.FileBean
public class FileBean
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 Summary | |
---|---|
FileBean(File file,
String contentType,
String originalName)
Constructs a FileBean pointing to an on-disk representation of the file uploaded by the user. |
|
FileBean(File file,
String contentType,
String originalName,
String charset)
Constructs a FileBean pointing to an on-disk representation of the file uploaded by the user. |
Method Summary | |
---|---|
void |
delete()
Deletes the temporary file associated with this file upload if one still exists. |
String |
getContentType()
Returns the content type of the file that the user selected and uploaded. |
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). |
InputStream |
getInputStream()
Gets an input stream to read from the file uploaded |
Reader |
getReader()
Gets a reader to read characters from the uploaded file. |
Reader |
getReader(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(File toFile)
Saves the uploaded file to the location on disk represented by File. |
protected void |
saveViaCopy(File toFile)
Attempts to save the uploaded file to the specified file by performing a stream based copy. |
String |
toString()
Returns the name of the file and the content type in a String format. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public FileBean(File file, String contentType, 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(File file, String contentType, String originalName, 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 requestMethod Detail |
---|
public String getFileName()
public String getContentType()
public long getSize()
public InputStream getInputStream() throws IOException
IOException
public Reader getReader() throws UnsupportedEncodingException, IOException
UnsupportedEncodingException
IOException
public Reader getReader(String charset) throws UnsupportedEncodingException, IOException
charset
- the charset the reader should use
UnsupportedEncodingException
IOException
public void save(File toFile) throws IOException
toFile
- a File object representing a location
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(File toFile) throws IOException
toFile
- the file to save to
IOException
public void delete() throws IOException
IOException
- if the delete will fail for a reason we can detect up front, or if
we try to delete and get a failurepublic String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |