File upload example

This example refers to the interceptor and injection concepts. Please read those documentations first.

You must also put the commons-io and commons-fileupload libraries in your classpath. These libraries are used by the MultipartRequestInterceptor, a interceptor already included to Vraptor, which offers a easy way to handle file uploading.

In our example we'll do the following steps:

  1. create a upload form
  2. write the business class
  3. write the business logic
  4. create page for the result

    Note: The upload file size is automatically limited to 2.000.000 bytes by VRaptor (and will be configurable in the feature).

The upload form

Before we start with coding let's write the html form for our upload:

<html>
<form method="post" ACTION="upload.doIt.logic" ENCTYPE='multipart/form-data'>
        File:<input type="file" name="fileInfo" ><br>
        <input type="submit" name="Submit" value="send"> 
        <input type="hidden" name="action" value="upload">
</form>
</html>

Please note ENCTYPE='multipart/form-data', which indicates that this form will create a multipart request and includes the selected file. Also observe that we call the file to upload fileInfo, which is important for our business logic.

MultipartRequestInterceptor

This interceptor handles the request, parses it and offers the uploaded file. It does all the 'dirty' work for us ...

So let's write a logic class which uses the MultipartRequestInterceptor:

package org.vraptor.examples.logic;

import org.vraptor.annotations.Component;
import org.vraptor.annotations.InterceptedBy;
import org.vraptor.interceptor.MultipartRequestInterceptor;

@Component
@InterceptedBy( MultipartRequestInterceptor.class )
public class UploadLogic {

        public void doIt() {
                //omitted
        }
}

As you already know, we use the @InterceptedBy annotation to tell VRaptor, it has to intercept our logic. So the MultipartRequestInterceptor will execute before any logic in class and parses the multipart request for us.

UploadedFileInformation

VRaptor will parse and look for a file in the request. But how do we get the file eventually?

We simple inject it. Actually, we inject a wrapper object.

The simple class UploadedFileInformation represent the wrapper. It has only some methods to access the uploaded file and to get some informations (eg. ContentType) about it.

A possible code could be:

@Component
@InterceptedBy( MultipartRequestInterceptor.class )
public class UploadLogic {


        //inject the wrapper 
        @In(required=false)
        private UploadedFileInformation fileInfo;
        
        public void doIt() {

                //exception handling omitted!!!!                
                //get the uploaded file
                File uploadedFile = fileInfo.getFile();
                System.out.println(uploadedFile.getAbsolutePath());
                
        }
}

That's all code to handle a file upload in VRaptor. Just inject the fileInfo and do what you want with it. All the nasty code is encapsulated by the MultipartRequestInterceptor.

Please note the required=false property of the @In annotation. As something can go wrong, VRaptor is not required to inject the fileInfo. That may also mean, you should validate the fileInfo and test it before you work on it. If you are in doubt, please read the validation tutorial.

Success Page

Now we display the result using VRaptors defaults. The page is upload/doIt.ok.jsp:

<html>
        The file was successfully uploaded.
</html>

Conclusion

Uploading a file in VRaptor is a piece of cake. You need to use the MultipartRequestInterceptor and inject the UploadedFileInformation. That's it. Anything else is not different to any other business logic VRaptor.