You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by "Schachter, Michael" <MS...@Bluestone.com> on 2000/10/03 21:09:52 UTC
Volunteer: File Upload/ Multipart form handling
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
Re: Volunteer: File Upload/ Multipart form handling
Posted by Alec Bau <Al...@msdw.com>.
> Ouch. That's probably OK for small files, but if someone is uploading a
> large file, or several people are uploading at once, isn't that going kill
> performance, as you eat up all the system's memory?
I was using a limit on file size (5Mb). In any case if you have a big
mutltipart form with file upload you don't want a user to wait
considerable time just to validate a form and report an error that they
missed some obscure but required field. Also reading into memory makes
this phase faster than writing immediately to the disk. For the site
with high probability of many simultaneous uploads you probably will
need to sync the code or limit concurrent use in any case or invest in
hardware and clustering.
Certainly if you have a simple one file field form with an Upload File
button then you want to go directly to saving file on disk. But then
you'll have nothing to populate and you can simply use any of available
file upload packages in your Action.
I think if we're going to integrate mutltipart stuff in Struts in a
generic way there should be a set of config params (maybe even on a per
form basis) that at least let specify how upload files should be handled
(memory or disk), what's the file size threshold for memory storage,
what's the directory and file name pattern for disk storage, etc. In
case of automatic disk storage there also should be a way to auto
rollback (delete file(s)) in case the form didn't pass validation.
Martin Cooper wrote:
>
> At 05:24 PM 10/3/00 -0400, Alec Bau wrote:
> >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
>
> Ouch. That's probably OK for small files, but if someone is uploading a
> large file, or several people are uploading at once, isn't that going kill
> performance, as you eat up all the system's memory?
>
> --
> Martin Cooper
> Tumbleweed Communications
Re: Volunteer: File Upload/ Multipart form handling
Posted by Martin Cooper <ma...@tumbleweed.com>.
At 05:24 PM 10/3/00 -0400, Alec Bau wrote:
>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
Ouch. That's probably OK for small files, but if someone is uploading a
large file, or several people are uploading at once, isn't that going kill
performance, as you eat up all the system's memory?
--
Martin Cooper
Tumbleweed Communications
Re: Volunteer: File Upload/ Multipart form handling
Posted by Alec Bau <Al...@msdw.com>.
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
above.
Thanks,
Alec
"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