Struts File Upload using Tag <html:file>

In this Struts example, you will learn how to upload a file Struts <html:file> tag. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Create JSP

Now we will create a HTML file upload tag using Struts’s HTML tag

Write the following code in fileupload.jsp

<%@taglib uri="" prefix="html"%>
<title>Struts <Html:file> File Upload</title>
<html:form action="/upload" method="post" enctype="multipart/form-data">
    <font color="red"><html:errors/></font><br>
    File : <html:file property="file" />

2. Action Form

Create a Action Form class and name it that contains the following code.

package com.kruders.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 static final long serialVersionUID = 1L;
    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){
            errors.add("fileError", new ActionMessage("error.file.required"));
            return errors;
        //only allow textfile to upload
            errors.add("fileError", new ActionMessage("error.file.type"));
            return errors;
        //file size cant larger than 10kb
        if(getFile().getFileSize() > 10240){ 
            errors.add("fileError", new ActionMessage("error.file.size"));
             return errors; 
        return errors;

3. Action Class (Controller)

Let us create an Action class that will handles and forward the request.

package com.kruders.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.kruders.form.FileUploadForm;
public class FileUploadAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FileUploadForm uploadForm = (FileUploadForm) form;
        FileOutputStream fos = null;
        FormFile formFile = null;
        try {
            formFile = uploadForm.getFile();
            String path = getServlet().getServletContext().getRealPath("")  + "/" + formFile.getFileName();
            fos = new FileOutputStream(new File(path));
        } finally {
            if (fos != null) {
        request.setAttribute("message", "The file " + formFile.getFileName()
                + " is uploaded successfully.");
        return mapping.findForward("success");

4. Properties File

Create and write the following text.

error.file.required = Please select a file to upload
error.file.type = Please select a text file to upload
error.file.size = File size should be less than 10 kb

5. Struts Config

Write the following code in struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
        <form-bean name="FileUploadForm" type="com.kruders.form.FileUploadForm"/>
        <forward name="upload" path="/"/>
        <action input="/upload.jsp" name="FileUploadForm" path="/upload"  validate="true" scope="request" type="com.kruders.action.FileUploadAction">
            <forward name="success" path="/success.jsp" />

6. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


7. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 27.1

Figure 27.1 Figure 27.1

When the user submits the form without selecting any file, following screen will be displayed as in Figure 27.2

Figure 27.2 Figure 27.2

When the user selects a file other than an txt file, following screen will be displayed as in Figure 27.3

Figure 27.3 Figure 27.3

The folder structure of the example is shown below in Figure 27.4

Figure 27.4 Figure 27.4

You can download the source code of this example here.


One Response to Struts File Upload using Tag <html:file>