Sturt2学习之十三---文件下载
1、download.jsp
<body>
<s:a href="/struts2/download.action">download</s:a>
</body>
2、DownloadAction.java
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport
{
public InputStream getDownloadFile()
{
return ServletActionContext.getServletContext().getResourceAsStream(
"/upload/Struts2.ppt");
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
3、在struts.xml中增加
注意:type="stream",若要深入了解,可参考struts-default.xml中stream的描述
<param name="inputName">downloadFile</param>是更具action的方法getDownloadFile配置的
<action name="download" class="com.test.action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-powerpoint</param>
<param name="contentDisposition">filename="Struts2.ppt"</param>
<param name="inputName">downloadFile</param>
</result>
</action>