struts2文件下载

struts.xml文件:

<action name="download" class="com.download.DownloadAction">
	<result type="stream" name="success">
		<param name="contentType">application/octet-stream</param>
		<param name="inputName">inputStream</param>
		<param name="contentDisposition">attachment;filename="${fileName}"</param>
		<param name="bufferSize">4096</param>
	</result>
</action>

 Action类:

package com.download;

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;
	
	private String fileName;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getInputStream() {
		return ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
	}

	public String execute() {
		return "success";
	}
}

 jsp代码:

<a href="download.action?fileName=yyy.xls" name="">下载文件</a>
(注意:yyy.xls文件在WebContent目录下)

你可能感兴趣的:(apache,jsp,xml,struts)