jsp页面
<body>
<h1>文件下载</h1>
<a href="download.action">文件下载</a>
</body>
action控制层
package com.imooc.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
public String inputPath;//文件下载的路径
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
@Override
public String execute() throws Exception {
System.out.println("123");
return SUCCESS;
}
public InputStream getInputStream(){
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
}
struts.xml
<action name="download" class="com.imooc.action.FileDownloadAction">
<param name="inputPath">/images/img3-lg.jpg</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="img3-lg.jpg"</param>
<param name="bufferSize">8192</param>
</result>
</action>