struts2 文件下载

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1、下载页面:


<body>
    <h4>this is the download.jsp</h4>
    <a href="download">文件下載</a>
  </body>

 


2、写对应action处理类:           


public class DownloadAction extends ActionSupport{
	private String filename ;
	
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	
	public InputStream getDownloadFile() throws UnsupportedEncodingException{
		filename = "downloadFile";
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/java泛型.docx");
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
}


3、在struts.xml中配置action:

<action name="download" class="com.strong.action.DownloadAction">
			<result name="success" type="stream">
				<param name="contentDisposition"> filename=${filename}</param>
				<param name="inputName">downloadFile</param>
			</result>
		</action>