Introduction
Hello Ninjas,
Welcome to Coding Ninjas Studio.
In this article, we will discuss the fileUpload Interceptor but before talking about the Interceptors, let us know about the Struts.
Struts
Craig McClanahan created the struts framework, which was donated to Apache Foundation in May 2000. In June 2001, the first version of Struts, i.e., Struts 1.0, was launched.
Now we are using Struts 2, which is used to develop Model View Controller (MVC)-based web applications.
Struts 2 is the combination of the webwork framework of opensymphony and struts 1.
It provides support to POJO-based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles, etc., and support to various result types such as Freemarker, Velocity, JSP, etc.
Interceptor
Interceptor is an object invoked during the preprocessing and postprocessing of a request. In Struts 2, It performs operations such as validation, exception handling, internationalization, displaying the intermediate results, etc.
In this article, we will discuss fileUpload Interceptor.
fileUpload Interceptor
The fileUpload Interceptor works for all the requests automatically that include files. The fielUpload Interceptor controls the working of file upload in struts2. For example, defining allowed types, maximum file size, etc.
Parameters of fileUpload Interceptor
Following are the two parameters defined for the fileUpload Interceptor.
- maximumSize - It indicates the maximum size of the file to be uploaded.
- allowedTypes - It indicates the allowed types, i.e., which file format to upload, like jpeg, jpg, png, etc.
Automatically added parameters
It automatically adds two parameters to the request:
- String fileName: It represents the filename of the file.
- String contentType: It specifies the content type of the file.
The fileName and contentType name depend on the request parameter for the file. If the filename is a name, you need to use nameFileName and nameContentType.
Now, let's see some examples of Image Upload using Struts2.