File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes validating file uploads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validating file uploads" Watch "validating file uploads" New topic
Author

validating file uploads

Kyle Cartmell
Greenhorn

Joined: Mar 20, 2006
Posts: 5
I have created a form that allows a user to upload a file, and I expected to see that the corresponding FormFile within my form bean would be null if the user neglected to choose a file to upload and simply clicked submit. Of course, I was wrong and it looks like I still receive a valid FormFile object regardless. Which is fine!

My problem is that I don't see an elegant way to determine whether or not the user actually uploaded a file other than looking at the filename or size attributed of the FormFile object. Additionally, the examples I have found for this procedure don't seem to address this matter at all, and assume that something has been uploaded.

Have I missed something very basic that explains why there isn't an obvious way to do what I'm trying to do or have I just not found the solution yet?

Any help would be appreciated!
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
In my code I check the fileSize property...here is an example:


- Brent
Kyle Cartmell
Greenhorn

Joined: Mar 20, 2006
Posts: 5
In my code I check the fileSize property...


I was afraid I'd have to resort to that!

At first glance (I'll tinker on my own, of course...) it would appear that this solution prevents a user from uploading an empty file.

Can anyone supply an alternative to this approach, or failing that provide me with some feedback on the idea of testing for a blank filename and a file size of zero to identify cases where no file was uploaded? (Can you have a blank filename but a size > 0? I suspect that there's a way...)

On a side note, any thoughts on why null isn't passed instead of a somewhat unpopulated FormFile object?

Thanks for the help!
[ March 21, 2006: Message edited by: Kyle Cartmell ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Is there a reason that a user would want to upload an empty file?

- Brent
Kyle Cartmell
Greenhorn

Joined: Mar 20, 2006
Posts: 5
Is there a reason that a user would want to upload an empty file?


Is there a reason why a user shouldn't be able to upload a blank file?

Okay, so it doesn't make sense in my application. However, the logic is still useful to me because I need to differentiate between no file having been uploaded and a blank file being uploaded so that my error messages make sense to the user and I don't have to field a support ticket when this happens. ("no file was uploaded!" vs. "uploaded file was empty!")
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I would show a message saying "The file you selected is invalid or empty" and move on to the next requirement.

- Brent
Kyle Cartmell
Greenhorn

Joined: Mar 20, 2006
Posts: 5
Huh. Point well taken.

Thanks for all the help, I appreciate it.
Kyle Cartmell
Greenhorn

Joined: Mar 20, 2006
Posts: 5
I do so enjoy the occasional beating of an ungulate corpse!

I found a very interesting comment in the Struts html taglib reference:

In order to correctly recognize uploaded files, the ActionForm bean associated with this form must include a statement setting the corresponding org.apache.struts.upload.FormFile property to null in the reset() method.


This suggests to me that setting my FormFile property to null in the reset method should produce the behavior I expected in the first place. Which, of course, it does not.

Any thoughts on this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validating file uploads