struts2文件下载功能,点击下载后不会弹出下载框(下载文件会在页面中显示出来)
struts代码:
<action name="download123" class="ui.action.DownloadAction">
<param name="path">/down/zwc.txt</param>
<result type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">
filename="zwc.txt"
</param>
<param name="inputName">inputStream</param>
</result>
</action>
jsp代码:
<a href="download123">下载</a>
action代码:
package ui.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private String path; //下载文件路径,在struts.xml中配置
public void setPath(String path) {
this.path = path;
}
public InputStream getInputStream(){
System.out.println(ServletActionContext.getServletContext().getResourceAsStream(path));
return ServletActionContext.getServletContext().getResourceAsStream(path);
}
public String execute(){
System.out.println(ServletActionContext.getServletContext().getResourceAsStream(path));
return SUCCESS;
}
}
点击下载后内容会显示输出到页面当中
[解决办法]
1楼的,在你的<param name="contentDisposition">attachment;filename="zwc.txt"</param>
只要加上attachment就行了,别的不改,我试了试!