How to upload & download a file in Struts framework

Hello friends, I worked on a project to create a website for jobs seeker & jobs provider using Struts framework which required file uploading & downloading of Resume.

Before I explain you the code, let me give u brief introduction of struts framework first:-

Struts is a free open-source framework for creating Servlet/JSP based web applications based on Model-View-Controller (MVC) architecture. The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code. The Struts framework is designed to help developers create web applications that utilize a MVC architecture.

you can use any IDE to develop your struts application like NetBeans, Eclipse or Weblogic.

First of all create a jsp named FileUpload

<title>Struts File Upload</title>
<html:form action=”/fileupload” method=”post” enctype=”multipart/form-data”>
<font size=”5″>File Upload on Server</font>
<font color=”red”><html:errors/></font>
File Name
<html:file property=”theFile”/>
<html:submit>Upload File</html:submit>

Now since we have used html tag library so you will have to add
taglib uri of html in case of Netbeans.

This jsp will have file type property from where u can browse the file on clicking submit the action fileupload will be called.

Now create a Bean package named beans & then create a class bean named StrutsUploadAndSaveForm which will extend ActionForm class

package beans;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
public class StrutsUploadAndSaveForm extends ActionForm
private FormFile theFile;

public FormFile getTheFile() {
return theFile;

public void setTheFile(FormFile theFile) {
this.theFile = theFile;

in the above code getTheFile function Returns the File &
setTheFile function set the file whose parameters are passes.

Now create action package named action and create a action class named StrutsUploadAndSaveAction which extends Action class

package action;
import bean.StrutsUploadAndSaveForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class StrutsUploadAndSaveAction extends Action
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception
StrutsUploadAndSaveForm myForm = (StrutsUploadAndSaveForm)form;
// Process the FormFile
FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();
//Get the file name
String fileName = myFile.getFileName();
byte[] fileData = myFile.getFileData();
//Get the servers upload directory real path name
String filePath = getServlet().getServletContext().getRealPath(“/”) +”upload”;
/* Save file on the server */
System.out.println(“Server path:” +filePath);
//Create file
File fileToCreate = new File(filePath, fileName);
//If file does not exists create file
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);


//Set file name to the request object

return mapping.findForward(“success”);


/* This action class first takes the file which is set in the bean. then its actual path is stored in the variable filepath.
then if the filepath is not empty, a new file with same name is created inside realpath of servlet or jsp/upload
if the filepath is empty than fileOutStream is flushed.
& in the end name of the file is set in the request scope such that the same file can be downloaded latter.

Now enter the Code in Struts Config such that their is right mapping of the code.

<form-bean name=”FileUpload” type=”beans.StrutsUploadAndSaveForm”/>



<action path=”/fileUpload” type=”action.StrutsUploadAndSaveAction” name=”FileUpload” scope=”request” validate=”true” input=”/FileUploadAndSave.jsp”>

<forward name=”success” path=”/downloadfile.jsp”/>




Now create a jsp named downloadfile.jsp

String fileName=(String)request.getAttribute(“fileName”);


<p align=”center”><font size=”5″ color=”#000080″>File Successfully Received</font></p>
<p align=”center”><a href=”upload/<%=fileName%>”>Click here to download</a></p>



8 comments so far

  1. Amit on

    thanx for the code it really works..
    can we also upload media files using this code??

  2. kunalsachdeva on

    @ Amit
    we can upload all type of files using this code including media files.

  3. Dan* on

    excellent post 🙂

  4. kamesh on


  5. Eddi on

    Thanks man this helped me to move on and modify this to save to the database.

  6. vijay on


  7. pie the number on

    What’s up to every , because I am actually keen of reading this web site’s post to
    be updated daily. It includes fastidious information.

  8. salon auto geneve 2016 on

    Howdy! This post could not be written any better!
    Reading this post reminds me of my previous room
    mate! He always kept chatting about this. I will forward this write-up to him.

    Pretty sure he will have a good read. Thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: