struts2 implements simple file download function

  • 2021-01-06 00:39:10
  • OfStack

Struts2 provides the stream result type, which is specifically used to support the file download functionality. Configuring the results of stream types requires you to specify the following four properties.

contentType: Specifies the file type of the downloaded file

inputName: Specifies the entry input stream for the downloaded file

contentDisposition: Specifies the file name to download

bufferSize: Specifies the buffer size for downloading files

struts2 file download example:

1. Action for handling file downloads:


/**
 * Description:Struts2 Control file download 
 * Author: Eleven
 * Date: 2018/1/24 10:39
 */
public class FileAction extends ActionSupport{

  // This member variable corresponds to struts.xml In the inputName The value of the , And provide them with get methods 
  private InputStream targetFile;

  // File download 
  public String download(){
    // Specifies the location of the downloaded resource and returns the corresponding input stream 
    String path = "/WEB-INF/images/lib.zip";
    // using getResourceAsStream() Converts the specified file to the corresponding input stream 
    targetFile = ServletActionContext.getServletContext().getResourceAsStream(path);
    return SUCCESS;
  }

  // provide get methods 
  public InputStream getTargetFile() {
    return targetFile;
  }
}

File download, first must have the downloaded file resource, here I put the downloaded file in the project WEB-INF/images path, can according to their own needs, and then directly use ServletContext provided getResourceAsStream() method to return the specified file corresponding to the input stream.

2. Configuration struts. xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="default" namespace="/" extends="struts-default">

    <action name="file_*" class="eleven.action.FileAction" method="{1}">
      <!-- File download -->
      <!-- The configuration result type is stream The results of the -->
      <result type="stream">
        <!-- Specifies the file type to download the file -->
        <param name="contentType">application/zip</param><!--image/jpg-->
        <!-- The specified action Returns the downloaded file from InputStream The name of the -->
        <param name="inputName">targetFile</param>
        <param name="contentDisposition">filename="aaa.zip"</param>
        <!-- Specifies the buffer size of the downloaded file -->
        <param name="bufferSize">4096</param>
      </result>
    </action>

  </package>

</struts>

In the browser address bar, type the corresponding file download access paths, such as http: / / localhost: 8080 / demo/file_download, is ready for download file. \


Related articles: