Struts file upload example

In this tutorial, you will learn how to upload a file to the server file system with Struts <html:file> tag.

Download this Struts file upload example –

1. Action Form

In Action form, create a org.apache.struts.upload.FormFile variable to hold the uploaded file, and also the form validation for the uploaded file.

package com.mkyong.common.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
public class FileUploadForm extends ActionForm{
	private FormFile file;
	public FormFile getFile() {
		return file;
	public void setFile(FormFile file) {
		this.file = file;
	public ActionErrors validate(ActionMapping mapping,
	   HttpServletRequest request) {
	    ActionErrors errors = new ActionErrors();
	    if( getFile().getFileSize()== 0){
	    	new ActionMessage("error.common.file.required"));
	       return errors;
	    //only allow textfile to upload
	    	 new ActionMessage("error.common.file.textfile.only"));
	        return errors;
            //file size cant larger than 10kb
	    if(getFile().getFileSize() > 10240){ //10kb
		    new ActionMessage("error.common.file.size.limit", 10240));
	       return errors;
	    return errors;

2. Action

In Action class, just get the uploaded file and save it into the server file system, and store the newly created file details into a session for later use.

package com.mkyong.common.action;
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;
import com.mkyong.common.form.FileUploadForm;
public class FileUploadAction extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	    HttpServletRequest request, HttpServletResponse response)
	    throws Exception {
	    FileUploadForm fileUploadForm = (FileUploadForm)form;
	    FormFile file = fileUploadForm.getFile();
	    //Get the servers upload directory real path name
	    String filePath = 
               getServlet().getServletContext().getRealPath("/") +"upload";
	    //create the upload folder if not exists
	    File folder = new File(filePath);
	    String fileName = file.getFileName();
	        System.out.println("Server path:" +filePath);
	        File newFile = new File(filePath, fileName);
	          FileOutputStream fos = new FileOutputStream(newFile);
		return mapping.findForward("success");

3. JSP

You have to set the encoding type of <html:form> tag to “multipart/form-data” and specify the HTTP method as “post“.


<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="bean"%>
<h1><bean:message key="label.common.title" /></h1>
<html:messages id="err_name" property="common.file.err">
<div style="color:red">
	<bean:write name="err_name" />
<html:messages id="err_name" property="common.file.err.ext">
<div style="color:red">
	<bean:write name="err_name" />
<html:messages id="err_name" property="common.file.err.size">
<div style="color:red">
	<bean:write name="err_name" />
<html:form action="/Upload" method="post" enctype="multipart/form-data">
<br />
	<bean:message key="label.common.file.label" /> : 
	<html:file property="file" size="50" />
<br />
<br />
	<bean:message key="label.common.button.submit" />


	File uploaded to : <%= request.getAttribute("uploadedFilePath") %>
	<a href="upload/<%= request.getAttribute("uploadedFileName") %>">
        Click here to download it</a>

4. struts-config.xml

Link all together

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
			<forward name="success" path="/pages/display.jsp"/>
		parameter="" />

Test it

Select a file and click on the submit button.


It will forward to display.jsp and display the uploaded file details.

Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: ionizer loans()

  • Pingback: HD Coloring Pages()

  • Pingback: electrician job description pdf()

  • Pingback: house blue()

  • Pingback: water ionizer pay plan loans()

  • Pingback: plumbers in colorado city tx()

  • Pingback: previous()

  • Pingback: electricians atlanta()

  • Pingback: pay per day loans plan()

  • Pingback: car parking()

  • Pingback: laan penge nu uden sikkerhed()

  • Pingback: car parking()

  • Pingback: lan penge her og lan penge nu()

  • Pingback: watch free movies online()

  • Pingback: Blue Coaster33()

  • Kiran

    But, on click of Browse button i want to open my own folder from PC on popup wondow to select a file. How can achieve this ?. As I know by default on click of ‘Browse’ button, then popup wondow takes to ‘My Documents’ folder or Previously what we choosen folder. But I want open specific folder(I provide the PATH, that PATH should be opned on popup wondow).

  • Mary

    Muy bien hecho, llevaba dias buscando un ejemplo concreto, ademas es muy facil y practico, muchas gracias

  • Bhupender

    I am using this code but it showing “argument type mismatch – had objects of type “java.lang.String” but expected signature “org.apache.struts.upload.FormFile”

    so plz can you tell me how to reslove it

  • Satish Kumar


    In this concept i have to store uploading file from one computer to another computer. can u please help any body………………


  • SweetFlow

    I am working on breakPoint upload,so I want to know how can I do this.when I use struts2 and fileupload,I encounter some problems.So I hope you can give me e road that I can finish my work.
    Thanks in advance,

  • Pingback: Struts 2 upload multiple files example()

  • Swathi

    Hi ,
    I need small help i uploded an image file in the jsp and my req is in need to display this image file in the jsp using struts….

    can anyone help me out ASAP….

    Thanks in advance,


    • rakesh

      Thanks Mkyong

  • Kane

    my service is in struts , JSP . The problem encountered is enctype=”multipart/form-data , seem to cause the object data which is set through request.setAttribute and passed through each Action class, is getting lost. Ultimately , not able to access request.getAttribute(..) .

    ANy suggestions on how to handle this.

  • onyou

    very clear and simple. you just made my day – thanks!

  • Allyson Ota

    Hi there, just became aware of your blog through Google, and found that it’s really informative. You can also share it at filebrid. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers!

  • Pingback: Struts 2 file upload example()

  • Pingback: Struts Tutorials | Tutorials()