ext + struts2 下载文件

 ext  右键菜单部分:

 

       {
                      id:'expversion',
                      text:'导出版本',
                      handler:function(){
                          var _url='fileDown.action';
                          if(_docvId!=null){
                              _url+='?docvId='+_docvId;
                          }else if(_docId!=null){
                              _url+='?docId='+_docId;
                          }
                          window.open(_url,'_self','width=1,height=1,toolbar=no,menubar=no,location=no');      //注意这里    用window.open() 否则不出现下载对话框
                 }}]
        }

 

 

struts2  Action:

 

private String downFileName;

public String execute() throws Exception {
        File docvfile = fileService.exportFile(docvId, docId);

        if (docvfile == null) {
            return null;
        }
        setDownFileName(docvfile.getName() + "." + docvfile.getSuffix());
        String path = docvfile.getPath();

        setFilePath(path.substring(path
                .lastIndexOf(savePath.replace("/", "//"))));
        // 文件下载目录路径

        String downloadDir = ServletActionContext.getServletContext()
                .getRealPath(savePath);

        // 文件下载路径
        java.io.File file = new java.io.File(path);

        path = file.getCanonicalPath();// 真实文件路径,去掉里面的..等信息

        // 发现企图下载不在 /download 下的文件, 就显示空内容

        if (!path.startsWith(downloadDir)) {

            return null;

        }

        return SUCCESS;
    }

    public InputStream getInputStream() throws Exception {
        return ServletActionContext.getServletContext().getResourceAsStream(
                filePath);
    }

 

public String getDownFileName() {//输出文件名 转码 中文 显示 问题
        try {
            return new String(downFileName.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            return downFileName;
        }
    }

    public void setDownFileName(String downFileName) {
        this.downFileName = downFileName;
    }

 

struts.xml:

 

<struts>
    <package name="fileDown" extends="struts-default">
       <default-action-ref name="download"/>
        <action name="fileDown" class="fileDownAction">
            <param name="savePath">/docUpload</param>
            <result type="stream">
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">
                    attachment;filename=${downFileName}//下载时使用的文件名 与action中 getDownFileName()对应
                </param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
    </package>
</struts>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值