1.download.jsp页面
<body>
<s:url id="url" action="download"></s:url>
<s:a href="%{url}">download file</s:a>
</body>
2.在webroot下创建一个文件,用于保存要下载的文件
如image
3.DownloadAction.java
package com.test.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport {
private String inputpath;
public void setInputpath(String inputpath) {
this.inputpath = inputpath;
}
public InputStream getInputStream() throws Exception
{
return ServletActionContext.getServletContext().getResourceAsStream(inputpath);
}
public String execute() throws Exception {
return SUCCESS;
}
}
4.struts.xml
<package name="testdownload" extends="struts-default">
<action name="download" class="com.test.action.DownloadAction">
<param name="inputpath">/image/Mode.rar</param>
<result name="success" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="Mode.rar"</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
posted on 2008-05-08 11:16
长春语林科技 阅读(424)
评论(0) 编辑 收藏 所属分类:
struts2