struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alec Bau <>
Subject Re: Volunteer: File Upload/ Multipart form handling
Date Tue, 03 Oct 2000 21:24:46 GMT
Michael, thanks for volunteering for this important addition!

I've implemented something like this overriding
ActionServlet.processActionForm method. I've used
com.jspsmart.upload.SmartUpload package and a modified
BeanUtils.populate method. In processActionForm I was checking if
request content type header is multipart and branched accordingly. I
think that's a more transparent approach than forcing special
MultipartActionForm interface.

There're some important gotchas to consider stemming mostly from the
fact that, at least in Tomcat 3.2, you can get ServletInputStream from
the same request only once! It gets discarded while you sequentially
read it. Also since there's no guarantee that parameter part(s) and file
part(s) are arranged in any specific order within request you can't
really separate param parsing for form validation and potentially very
lengthy file contents part(s) parsing. Ideally latter should be delayed
until explicitly requested in Action perform method if form was
successfully validated.

As a result:

1. At the parsing stage in processActionForm I've chosen to store
file(s) contents in *memory* and not write it to file(s). Otherwise if
form validation wasn't ok or requested action in a multiaction form
wasn't upload you'll require user to deal with deleting file(s). Also
there're lot of uses where it's required to postprocess contents before
saving (if at all), save in db, change name, etc. So I decided to store
Uploader object as a request attribute. Then in Action.perform user if
needed can retrieve Uploader object from request and get InputStream(s),
mime type(s), etc. and do whatever processing is needed.

2. I've wrote a separate BeanUtils.populate method (based on regular
one) that gets param values from Hashtable instead of
HttpHttpServletRequestRequest and handles prefixes and postfixes.

Please let me know if you need more info or source code for the stuff


"Schachter, Michael" wrote:
> I'd like to volunteer to create a more abstract way of handling file uploads
> through struts, basically through creating a new
> "org.apache.struts.action.MultipartActionForm" interface.
> People implementing form action classes with "multipart/form-data" enctypes
> would implement this class instead of "ActionForm".  They'd declare the
> usual setter and getter methods for their text form entries, and declare
> setter and getter methods for inputs of type "file", the getter methods
> returning an object of type "File", and the setter methods taking a
> parameter of type "File".
> on jsp page:
> <struts:form enctype="multipart/form-data" ect...>
>     <struts:file name="theFile" />
> </struts:form>
> in implementation of MultipartActionForm:
> public File getTheFile() {;}
> public void setTheFile(File theFile) {;}
> I figure I'll have to make this change somewhere inside of
> "BeanUtils.populate(Object, String, String, HttpServletRequest" when the
> ActionForm instance is populated, but I'm not totally done with planning
> this out yet.  Suggestions/constructive criticism will be appreciated

View raw message