日期:2014-05-17 浏览次数:20673 次
package com.company.action; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.Action; @Results( { @Result(name = "success", type = "stream", params = { "contentType", "application/x-msdownload", "inputName", "inputStream", "contentDisposition", "attachment;filename=%{url}", "bufferSize", "5120" }) }) public class DownAction implements Action { // 文件地址 private String url; public InputStream getInputStream() throws Exception { InputStream inputStream = ServletActionContext.getServletContext() .getResourceAsStream(url); return inputStream; } /**** * 默认执行下载文件 */ public String execute() throws Exception { return SUCCESS; } public String getUrl() { return url; } public void setUrl(String url) { // 进行中文转码 try { url = new String(ServletActionContext.getRequest().getParameter( "url").getBytes("iso-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } this.url = url; } }