Child pages
  • File Upload Interceptor
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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:

  1. 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.
  2. 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.
  3. 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?

  • No labels