The all-new ACE FileEntry component introduces in-memory file processing. This feature is designed to provide for antivirus scanning, checksum verification or a scenario where the upload doesn't need to be saved to disk, and doing so immediately is excessive IO.
The functionality requires that a listener bean implement FileEntryCallback to handle the incrementally uploaded bytes.
Instances of FileEntryCallback must implement:
This example demos in-memory processing to provide an MD5 hash for an uploaded file.
|There are no images attached to this page.|
Creating our handler is the most important step of this tutorial. Everything else is just wiring up the dependant parts.
|Check fileInfo.getStatus() to determine if the file has pre-failed uploading, due to too many files uploaded, an invalid file extension, or content type.|
The MessageDigest class is an Apache Commons class for using one-way hash algorithms like MD5 and SHA.
The begin method is where your callback should request any resources it will require.
The example sets up a MessageDigest object.
These methods will be passed portions of file data. If there is a chance the file will eventually be saved, the byte input should be cached here.
This example immediately computes the data as part of our hash.
This method triggers when a file upload is complete, either successfully or with errors.
If you want to invalidate a successful upload, perhaps it's over quota, then this method should handle the case. It may massage the result (raising some prompts and accepting something), or possibly fail the upload for good by calling FileInfo.updateStatus(...) with the status "FileEntryStatuses.INVALID".
The example below either prints a custom success FileEntryStatus, a custom MD5 error status, or, in other fail cases, lets the failed status passed to end() propagate to the view.
Nothing special is required in the facelet of this tutorial. The sample below just has some simple styling and an EL reference to the callback bean.
That concludes this overview of the new in-memory processing feature. If you're interested in further details of how this example works, take a look at the complete source code below; or sign up for ICEfaces training for a complete guide to this example and every feature of ICEfaces!