This article has shared with you the file download function under struts2 for your reference. The specific content is as follows
Here is an example of implementing an image download function:
1. Project structure
2. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- Settings 2 filters--> <filter> <filter-name>struts 2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts 2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Settings Welcome Page--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- session timeout definition, unit in minutes--> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
3.DownloadAction.java
package com.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport{ private static final long serialVersionUID = 1L; //File path private String path; //Getter method of path attribute public String getPath(){ return path; } //Setter method of path attribute public void setPath(String path){ this.path = path; } //Return inputstream, key method for downloading files public java.io.InputStream getDownloadFile() throws Exception{ InputStream in = ServletActionContext.getServletContext().getResourceAsStream(getPath()); return in; } public String execute() throws Exception{ return SUCCESS; } }4.struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- Configure constants in Struts 2 applications --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- Configure the maximum capacity of uploaded files, struts2 is 2M by default. The unit is 1B, 1KB=1024B,1M=1024KB,1M=1024*1024B--> <constant name="struts.multipart.maxSize" value="1048576" /> <!-- Configure the package in this application and inherit the struts-default package--> <package name="FileDownload" namespace="/" extends="struts-default"> <action name="download"> <!-- Set the parameters of the file path and pass it to the action class file--> <!-- <param name="path">/download/a.jpg</param> --> <!-- Download file type definition, that is, defined as "stream" --> <result name="success" type="stream"> <!-- image/jpeg represents JPG image --> <param name="contentType">image/jpeg</param> <!-- Download file processing method --> <param name="contentDisposition"> <!-- attachment means attachment, that is, the save dialog window is opened during download, and filename represents the file name displayed during download-> <!-- If you do not write attachment; or write inline; it means inline, that is, you will try to open the downloaded file in the browser, rather than download--> attachment;filename="a.jpg" </param> <!-- Download file output stream definition--> <!-- The value corresponding to the inputName element here is downloadFile. In the action, you must have the corresponding getDownloadFile() method--> <param name="inputName">downloadFile</param> <!-- Download buffer size--> <param name="bufferSize">1024</param> </result> </action> </package> </struts>
5.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>Home</title> </head> <body> <center> Welcome to the homepage, click the link below to download a file<br /> <a href="download.action?path=<%="rel="external nofollow" ./download/a.jpg" %>">Download</a> </center> </body> </html>
6. File path
The download directory should be created in advance in the project, and there should be a.jpg file inside it, otherwise the download will fail.
7. Functional portal
After the project is published to the server, use the browser to access the index.jsp in the project, click the download link, and the "Download" dialog box will pop up.