标签:
Action:
package com.tengfeiyang.action; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; /**
* @author yangtf * 文件下载 16/02/23 */ public class FileDownloadAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getDownloadFile()throws Exception{ String path; if ("Java".equals(fileName)) { path = "C:\\Users\\yangtf\\Desktop\\Java.pdf"; // 绝对路径 File file = new File(path); this.fileName = "JavaAnalysisReport.pdf"; return new FileInputStream(file); } if ("PHP".equals(fileName)) { path = "C:\\Users\\yangtf\\Desktop\\PHP.pdf"; File file = new File(path); this.fileName = "PHPAnalysisReport.pdf"; return new FileInputStream(file); } return null; } @Override public String execute() throws Exception { return SUCCESS; } }
XML:
<action name="fileDownload" class="com.tengfeiyang.action.FileDownloadAction"> <result name="success" type="stream"> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="inputName">downloadFile</param> <param name="bufferSize">1024</param> </result> </action>
jsp:
<a href="fileDownload.action?fileName=Java">Java分析文档下载</a><br> <a href="fileDownload.action?fileName=PHP">PHP分析文档下载</a>
标签:
原文地址:http://www.cnblogs.com/tf-Y/p/5213431.html