Article updated on

Struts 2 Download File Annotations Example

Examples on how to download a file from Struts2 with annotations only.

Features:

  • Example 1 direct file download
  • Example 2 dynamic file download with parameter
  • Error page with 404 file not found
  • Takes path from Servlet context

Example 1

Example 2

Error not found page

Download deployable war file with sources here

 

Example 1

package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/")
@Results({ @Result(name = "error", location = "/error", type = "redirect") })
public class ExampleDownload1 extends ActionSupport {
    private static final long serialVersionUID = -7434672720094336409L;
    private static ServletContext servletContext = ServletActionContext.getServletContext();
    private static String webContentRoot = servletContext.getRealPath("/");
    private static final String pathToFile = webContentRoot + "zip/file.zip";
    private InputStream inputStream;
    @Action(value = "/file.zip",
            results = { @Result(name = "success", type = "stream",
                params = { "contentType", "application/octet-stream",
                            "bufferSize", "1024"
                            }) })
    public String execute() throws Exception {
        try {
            inputStream = new FileInputStream(new File(pathToFile));
        } catch (Exception e) {
            return ERROR;
        }
        return SUCCESS;
    }
    public InputStream getInputStream() {
        return inputStream;
    }
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}

 

Example 2

package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/")
@Results({ @Result(name = "error", location = "error", type = "redirect") })
public class ExampleDownload2 extends ActionSupport {
    private static final long serialVersionUID = -7434672720094336410L;
    private static ServletContext servletContext = ServletActionContext.getServletContext();
    private static String webContentRoot = servletContext.getRealPath("/");    
    private InputStream inputStream;
    private String fileName;
    @Action(value = "/download",
            results = { @Result(name = "success", type = "stream",
                params = { "contentType", "application/octet-stream",
                            "inputName", "inputStream",
                            "bufferSize", "1024",
                            "contentDisposition","filename=\"${fileName}\"" }) })
    public String execute() throws Exception {
        String pathToFile = webContentRoot + "zip/" + fileName;
        try {
            inputStream = new FileInputStream(new File(pathToFile));
        } catch (Exception e) {
            return ERROR;
        }
        return SUCCESS;
    }
    public InputStream getInputStream() {
        return inputStream;
    }
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

 

Not Found (Error) Action

package com.test.action;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class Error extends ActionSupport{
    private static final long serialVersionUID = 2086900467866735407L;
    @Actions({
        @Action(value = "/error", results = @Result(name = "success", location = "/error.jsp")) })
        public String execute() {    
        ServletActionContext.getResponse().setStatus(
                HttpServletResponse.SC_NOT_FOUND);
        return SUCCESS;
    }    
}