VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/struts-2-file-upload/

⇱ Struts 2 File Upload Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Struts 2 File Upload Example

Last Updated : 23 Jul, 2025

The Struts 2 framework has built-in functionality for handling file uploads via "Form-based File Upload in HTML". When a file is uploaded, it is normally kept in a temporary directory, and your Action class should process or relocate it to a permanent location to prevent data loss. File uploading is possible in Struts using a pre-defined interceptor named FileUploadInterceptor, which is available through the org.apache.struts2.interceptor.FileUploadInterceptor class and is included in the default stack. Example of action mapping:

<action name="doUpload" class="com.example.UploadAction">
 <result name="success">good_result.jsp</result>
</action>

Parameters of fileupload Interceptor

  • allowedTypes: Lists the permitted types. Image/png, image/jpg, etc. might be used.
  • maximumSize: Indicates the largest file size that may be submitted.
  • allowedExtensions: A comma-separated list of file extensions (for example,.html) for which the interceptor will allow a file reference to be specified on the action. If none is supplied, enable all extensions to be uploaded.

Struts 2 File Upload Example

1. Create View Files

This JavaScript website uses struts UI tags to generate a form. The user sends it a file.

2. Make the SuccessUserImage.jsp file

Built with Struts UI tags, this website offers a user-friendly form for you to submit your name, password, and email address.

3. Make the action class

The execute function is overridden by this action class, which derives from the ActionSupport class.

4. Create struts.xml

This XML defines an interceptor named "jsonValidatorWorkflowStack" and associates it with the "input" result.

Output:

👁 fileupload1

👁 Final Result

Conclusion

So this is Struts 2 File Upload Example. The Struts 2 framework has built-in functionality for handling file uploads via "Form-based File Upload in HTML". When a file is uploaded, it is normally kept in a temporary directory, and your Action class should process or relocate it to a permanent location to prevent data loss.

Comment

Explore