struts type="stream"方法实现下载

public class DownLoadAction extends ActionSupport
{
    private String downPath;        // 下载时的文件名
    private String contentType;     // 保存文件类型
    private String filename;        // 保存时的文件名
    private String directory = ""public String getDirectory()
    {
        return directory;
    }

    public void setDirectory(String directory)
    {
        this.directory = directory;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getDownPath() {
        return downPath;
    }
    public void setDownPath(String downPath) {
        try {
            // 解决下载时候的中文文件乱码问题
            downPath = new String(downPath.getBytes("ISO-8859-1"),"UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        this.downPath = downPath;
    }

    /*
     *下载用的Action返回一个InputString实例,该方法对应Action配置
     *里面的result的inputName参数,值为inputString
     *
     */

    public InputStream getInputStream(){
        //directory+"/"+downPath路径一定要正确,否则会报错
        //Exception occurred during processing request: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. 
        //Check the <param name="inputName"> tag specified for this action.
        return ServletActionContext
                .getServletContext().getResourceAsStream(directory+"/"+downPath);
    }

    public String execute(){
        // 下载保存时的文件名和被下载的文件名一样
        filename = downPath;
        // directory必须是工程下的文件夹
        downPath = directory+"/"+downPath;
        // 保存文件的类型
        log.info("directory="+directory+" filename="+filename+" downPath="+downPath);
        contentType = "application/octet-stream";
        return SUCCESS;
    }
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="download" extends="struts-default" namespace="/download">
        <action name="DownLoadAction" class="download.action.DownLoadAction">
            <result type="stream">
                <param name="contentType">${contentType}</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename=${filename}</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
    </package>
</struts>

jsp

 <a href="/download/DownLoadAction.action?downPath="文件名">文件名</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值