concept fileupload interceptor in category java

appears as: fileUpload interceptor, The fileUpload interceptor
Struts 2 in Action

This is an excerpt from Manning's book Struts 2 in Action.

As with most tasks that you find yourself doing routinely, Struts 2 provides built-in help for file uploading. In this case, the default interceptor stack includes the FileUploadInterceptor. As you might recall, struts-default.xml is the system file that defines all of the built-in components. Listing 3.10 shows the elements from that file that declare the fileUpload interceptor and make it a part of the default interceptor stack.

Listing 3.10. Declaring the FileUploadInterceptor and adding it to the stack
<package name="struts-default"> <interceptors> . . . <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> . . . </interceptors> . . . <interceptor-stack name="defaultStack"> . . . <interceptor-ref name="model-driven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="params"/> . . . </interceptor-stack> </package>

As you can see, the struts-default package contains a declaration of the fileUpload interceptor, backed by the org.apache.struts2.interceptor.FileUploadInterceptor implementation class. This interceptor is then added to the defaultStack so that all packages extending the struts-default package will automatically have this interceptor acting on their actions. We make our Struts 2 Portfolio packages extend this package to take advantage of these built-in components.

3.5.2. What does the fileUpload interceptor do?

The fileUpload interceptor creates a special version of the automatic data transfer mechanisms we saw earlier. With the previous data transfers, we were dealing with the transfer of form field data from the request to matching JavaBeans properties on our action objects. The params interceptor, also part of the defaultStack, was responsible for moving all of the request parameters onto the action object wherever the action provided a JavaBeans property that matched the request parameter’s name. In listing 3.10, you can see that the defaultstack places the fileUpload interceptor just before the params interceptor. When the fileUpload interceptor executes, it processes a multipart request and transforms the file itself, along with some metadata, into request parameters. It does this using a wrapper around the servlet request. Table 3.2 shows the request parameters that are added by this fileUpload interceptor.

After the fileUpload interceptor has exposed the parts of the multipart request as request parameters, it’s time for the next interceptor in the stack to do its work. Conveniently, the next interceptor is the params interceptor. When the params interceptor fires, it moves all of the request parameters, including those listed in table 3.2, onto the action object. Thus, all a developer needs to do to conveniently receive the file upload is add JavaBeans properties to her action object that match the names in table 3.2.