We can create a nice reusable Interceptor which can hadle file uploads absolutely transparently and Action will not know anything about web-app and just gets its files:
- Before further invocation it scans multipart request for files, and if content-type and size is acceptable it puts collected java.io.File instance to invocationContext.parameters Map named according to their <input> names.
- In Action we can just declare properties of type File which will be set by parameters Inteceptor, in execute() we can move file with aFile.renameTo(...) to the right place, or just read the it and leave alone.
- After invocation it removes all uploaded files via aFile.delete(). (Action should not care for iles uploaded waste disk space)
This Interceptor may be configured to filter files with certain mime type or size, and of course to be applied to any action(s) or even included into stack.
This removes (duplicate) web-app specific code from Action and gives ua a nice reusable component that handles 90% of all typical file upload tasks. Neat. Also illustrates power of Interceptor concept of XWork/WebWork2. Try to do this in Struts. 8)
Things to improve: error handling, reporting & i18n.
Example Page code:
Example Action code:
If this Interceptor considered generally useful - may be it will be incorporated into WW2 codebase?