File Uploads

Skip to end of metadata
Go to start of metadata

Stripes makes it very easy to manage file uploads. At its simplest you just include a file field in a stripes form, and match it with a FileBean property on your ActionBean. For example:

Using a file field on a JSP
<stripes:form>
    ...
    <stripes:file name="newAttachment"/>
    ...
</stripes:form>
A FileBean property in an ActionBean
private FileBean newAttachment;

public FileBean getNewAttachment() {
    return newAttachment;
}

public void setNewAttachment(FileBean newAttachment) {
    this.newAttachment = newAttachment;
}

At this point, it would be worth going and taking a look at the JavaDoc for FileBean.

Processing Uploaded Files

Unfortunately, we're not quite done yet. Due to limitations in the HTTP specification, the way file uploading is handled may not be quite as simple as you might at first think. It is worth taking the time to understand this. While the HTTP specification defines the multipart/form-data MIME type to allow the uploading of one or more files at the same time as other request data, it does not specify that the parts of the request must come in any specific order. This means that the browser can choose to send the files for upload before any of the other request parameters (read: information in your form).

What this means is that uploaded files cannot be streamed directly to your ActionBean, because you may not be able to access other request information until after the file is uploaded. To get around this Stripes, like other implementations, streams the files to a disk, processes the request and then provides you with access to the files on the local disk. This has repercussions for how you handle the FileBean objects that get attached to your ActionBean. Generally you're going to want save the uploaded file somewhere, or read it in as a stream and do something else with it...

Saving an uploaded file

Saving the uploaded file to a specific location can be achieved simply by calling the FileBean.save(File toFile) method. All this method does is move the temporary file saved during the upload processing to the location specified by the File you provide. If you invoke this method, since the temporary file gets moved, there is no need (in fact there is a need not) to call FileBean.delete().

Reading the uploaded file as a Stream

The alternative is to process the uploaded file as an InputStream. You might choose to do this if you want to stream the contents of the file to a database, or analysze the data that is uploaded. You can do this by calling FileBean.getInputStream() and then reading the data as you would with any other InputStream. Once you are done you should call FileBean.delete() to delete the temporary file. If you don't do this then your web application's temporary directory will fill up with all the uploaded files!

Know when to delete FileBeans
If you process a FileBean as an InputStream, call FileBean.delete(). If you use the save() method, don't call FileBean.delete()!

Limiting the size of uploads

Unfortunately the HTTP specification gets in the way again here. The only piece of useful information available before the contents of the POST is the total size of the POST. This includes all uploaded files, all other fields in the form, any headers etc. As a result, the only thing that we can use to limit uploads is the POST size (duh!). You'll want to consider a limit carefully - it should be low enough to prevent denial of service attack, but high enough to let your users upload the kinds of files you want them to.

By default Stripes set this limit to 10 megabytes. For information on how to change the limit refer to the File Upload section in the Configuration Reference.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.