Struts & file uploads

So Sean and I ran into an interesting problem involving Struts and uploading a file. The web application we’re currently working on requires file upload functionality. Our Q/A contact brought to our attention that uploading of files greater than 5MB wasn’t working. Not only that, but the users weren’t getting any errors at all and the page would just reload (of course the file wouldn’t be uploaded).

My first thought was to throw a breakpoint into the struts action on the first line of the executeSubmit(…) method and walk through the code to see exactly where we were falling over. Lo and behold, when we hit submit, we didn’t hit the breakpoint! Yikes! This was very surprising, if not completely confusing. How are we being redirected back to the upload page without hitting our struts action? Surely our action is doing the redirection no?


So here’s what the HTML looks like; really a standard form, except the type of the input is “file”.

<form id="uploadFileForm" name="uploadFileForm" action="uploadFile.html" method="post" enctype="multipart/form-data">
    <input id="uploadFile" size="60" name="uploadFile" type="file">


HTML Forms anyone?

We decided to then look online documentation for forms and type=”file” input. Not only was our HTML correct, but there also wasn’t any way to specifically limit the size of the file being uploaded in the HTML source; not surprising, but we figured we’d check it out anyway. So the HTML is properly formed and yet we’re still not hitting our breakpoint? Um, ok?

When it doubt, Google it

Well, it was time to consult the all knowing and powerful Oracle because we needed some answers. Just what the heck is going on? Does she have any candy? Is neo really the one? (because that bald kid in the waiting room who bent the spoon with his mind had some mad skills). Why was The Matrix Reloaded so confusing? … Anyhoo, back on point. A Google search about uploading files from the web and maximum size restrictions yielded some interesting results. Sean found a page detailing how PHP had a setting where it limited the size of files that were allowed to be uploaded. So we thought, hey, I wonder if Struts does that too?

Bad Struts, very bad!

So Sean did a search for “maxFileSize” and guess what? Struts has a property that limited file sizes! Below is the entry in struts-config.xml…

<controller maxFileSize="2M" processorClass="someCompany.web.struts.SomeRequestProcessor">

So in the end, any file over 2 megs just wouldn’t get uploaded. Not only that, but we got no exception or anything to indicate that this was enforced. Isn’t that just awesome? Um, no, not really.

The Fix

So the fix involved 2 things…

Struts Controller

The entry in the struts config is no good because we use xdoclet to generate the struts-config.xml, changing the entry in struts-config will eventually get blown away, so the fix really belonged in struts-controller.xml. We increased to max size to something larger than the limit we wanted to enforce…

<controller maxFileSize="20M" processorClass="someCompany.web.struts.SomeRequestProcessor">
    <set-property property="inputForward" value="true"/>
    <set-property property="nocache" value="true"/>

The Struts Action

So why make the struts file limit larger than what we really want? Well, it would be nice to, I don’t know, tell the user the file is too big! (just a thought). Let’s just say reloading the exact same page with no feedback whatsoever is somewhat undesirable.

So the limit in the struts controller is really a final resort and we added validation in our form informing the user that any file over 10 megs was a no go…

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    if (file.getFileSize() > 10000000) {
        errors.add(Globals.ERROR_KEY, new ActionMessage("error.uploadFile.tooBig"));

Figured this was worth a post as I’m sure someone else may very well run into this again.

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply