Class FileUpload
- java.lang.Object
-
- org.apache.wicket.markup.html.form.upload.FileUpload
-
public class FileUpload extends Object
Model for file uploads. Objects of this class should not be kept between requests, and should therefore be marked astransient
if they become a property of an IModel.- Author:
- Jonathan Locke
-
-
Constructor Summary
Constructors Constructor Description FileUpload(org.apache.commons.fileupload.FileItem item)
Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
closeStreams()
Close the streams which has been opened when getting the InputStream usinggetInputStream()
.void
delete()
Deletes temp file from diskbyte[]
getBytes()
String
getClientFileName()
String
getContentType()
byte[]
getDigest(String algorithm)
Get the MD5 checksum.InputStream
getInputStream()
Get an input stream for the file uploaded.byte[]
getMD5()
Get the MD5 checksum.long
getSize()
void
writeTo(File file)
Saves this file upload to a given file on the server side.File
writeToTempFile()
Convenience method that copies the input stream returned bygetInputStream()
into a temporary file.
-
-
-
Constructor Detail
-
FileUpload
public FileUpload(org.apache.commons.fileupload.FileItem item)
Constructor- Parameters:
item
- The uploaded file item
-
-
Method Detail
-
closeStreams
public final void closeStreams()
Close the streams which has been opened when getting the InputStream usinggetInputStream()
. All the input streams are closed at the end of the request. This is done when the FileUploadField, which is associated with this FileUpload is detached.If an exception is thrown when closing the input streams, we ignore it, because the stream might have been closed already.
-
delete
public void delete()
Deletes temp file from disk
-
getBytes
public byte[] getBytes()
- Returns:
- Uploaded file as an array of bytes
-
getDigest
public byte[] getDigest(String algorithm)
Get the MD5 checksum.- Parameters:
algorithm
- the digest algorithm, e.g. MD5, SHA-1, SHA-256, SHA-512- Returns:
- The cryptographic digest of the file
-
getMD5
public byte[] getMD5()
Get the MD5 checksum.- Returns:
- The MD5 checksum of the file
-
getClientFileName
public String getClientFileName()
- Returns:
- name of uploaded client side file
- Since:
- 1.2
-
getContentType
public String getContentType()
- Returns:
- Content type for upload
-
getInputStream
public InputStream getInputStream() throws IOException
Get an input stream for the file uploaded. Use this input stream if you can't usewriteTo(File)
for persisting the uploaded file. This can be if you need to react upon the content of the file or need to persist it elsewhere, i.e. a database or external filesystem.PLEASE NOTE!
The InputStream return will be closed be Wicket at the end of the request. If you need it across a request you need to hold on to this FileUpload instead.- Returns:
- Input stream with file contents.
- Throws:
IOException
-
getSize
public long getSize()
- Returns:
- The upload's size
-
writeTo
public void writeTo(File file) throws Exception
Saves this file upload to a given file on the server side.- Parameters:
file
- The file- Throws:
Exception
-
writeToTempFile
public final File writeToTempFile() throws Exception
Convenience method that copies the input stream returned bygetInputStream()
into a temporary file.Only use this if you actually need a
File
to work with, in all other cases usegetInputStream()
orgetBytes()
- Returns:
- temporary file containing the contents of the uploaded file
- Throws:
Exception
- Since:
- 1.2
-
-