File uploading is a challenging task in all the technologies where the developer needs to add more files and has to set the path to store the uploaded files. Struts 2 File Upload automatically work for all the requests and includes files. By using the Struts 2 File Upload Interceptor, which is available in the
org.apache.struts2.interceptor.package.FileUploadInterceptor
class and included as a part of the
defaultStack. Following is an example.
1. Upload the file, take the index file in enctype
multipart/form-data and form name is myFile.
2. Forwarded the request to Action class,
Action class extends
ActionSuppot class. Then that Action class contains some predefined properties
FileContentType,
FileName and
DestPath.
Properties Description
1. FileName - indicates the given file name, like splessons.jpg.
2. FileContentType - indicates which type images are used.like image/jpeg, image/png.
3. destPath - indicates which location the file is saved, like c/programfiles/ApacheSoftwareFoundation/web-app/.
3. One need to configure the
file size and
file type in struts.xml, Then FileAction class gives the response to View pages, like success page or JSP page.