The fileUpload interceptor automatically works for all the requests that includes files.
We can use this interceptor to control the working of file upload in struts2 such as defining allowed types, maximum file size etc.
Parameters of fileupload interceptor
There are 2 parameters defined for fileupload interceptor.
Parameter | Description |
---|---|
maximumSize | specifies maximum size of the file to be uploaded. |
allowedTypes | specifies allowed types. It may be image/png, image/jpg etc. |
It automatically adds 3 parameters in the request:
- File file represents the file. We can apply methods on this object.
- String fileName represents the filename of the file.
- String contentType specifies the content type of the file.
Image upload example using struts 2
Let's see the directory structure of file upload application.
1) Create UserImage.jsp
This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.
index.jsp2) Create SuccessUserImage.jsp
This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.
SuccessUserImage.jsp3) Create the action class
This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java4) Create struts.xml
This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.
struts.xml