This tutorial provides a step by step guide on how to implement upload file functionality with Struts 2 framework. With Struts 2, uploading files is easy to implement since the framework provides fileUpload interceptor which intercepts multipart request and makes uploaded files automatically available for Struts action class.

We are going to build a simple Java web application based on Struts 2 framework using Eclipse IDE. It provides a simple upload form in which the user can pick up a file to upload. The application will be deployed on Tomcat server, and the uploaded file will be copied to a specific directory on the server.

Suppose you have these pieces of software installed on your computer:

Table of content:

1. Setup Eclipse project
2. Configure Tomcat
3. Configure web.xml
4. Configure log4j
5. Configure Struts 2
6. Code upload form
7. Code Struts 2 action class
8. Code result page
9. Test the application
10. References

Attachments:
Download this file (StrutsFileUploader.zip)StrutsFileUploader.zip[Eclipse project]3845 kB
avatar
In the doUpload() method of the action class, change this line:

File saveFilePath = new File("E:/Upload/" + fileUploadFileName);

to:

String timeStamp = "calculate timestamp yourself";
File saveFilePath = new File("E:/Upload/" + fileUploadFileName + timeStamp);
VOTES:0
avatar
how to save the file in Tomcat/WEB-INF/Upload/ folder instead E:/Upload/
VOTES:0
avatar
It's not recommended to save the file under web application's directory because the file might be deleted if the application is un-deployed or re-deployed.
VOTES:0
avatar
how to add datetimestamp to filename while uploading file.?????
VOTES:0
avatar
Hi,
Anyone can help me? that i need to display the image in another jsp through struts2 action.
VOTES:0
avatar
Hi Nirmal,

What have you tried? What the problems are you facing?
VOTES:0
avatar
how create a directory path in Struts2
when im upload file i want to save into file like "WebConten/File"
with one small example .
VOTES:0
avatar
Hi Ravi,

You can change the location where the file are uploaded in the Struts action class, by modifying this line:
File saveFilePath = new File("E:/Upload/" + fileUploadFileName);
VOTES:0
Learn on Udemy Today!