• javaweb-JSP action中附件下载的写法


     附件下载(包括图片,exl,word)在前台必须给出一个iframe 或者类似于window的窗口,另外,Java文件下载不能通过ajax进行请求,必须添加src属性
    首选,前台的链接拼接html如下:

    '<a id="attach_name'+index+'" style="100%" class="downloadAttach btn btn-primary"><i class="fa fa-file"></i> '+row.ATTACH_NAME+'</a>'+
    '<iframe id="downloadFrame'+index+'" style="display:none;"></iframe>'

    然后click事件如下:

    //TODO 事件委托,下载附件按钮
    $(".taskRows").on("click",".downloadAttach",function(){
    var attachIndex = $(this).attr("id")//获取当前点击的元素ID
    attachIndex = attachIndex.charAt(attachIndex.length-1);
    var attach_name = $.trim($("#attach_name"+attachIndex).text());
    alert(attach_name);
    //根据附件名下载附件
    if(attach_name!=null &&attach_name!=undefined)
    {
    //$("#downloadFrame"+attachIndex).attr("src",baseCtx+"/wpgl/downloadAttachFile.action?attachName="+attachName);
    $("#downloadFrame"+attachIndex).attr("src",baseCtx+"/wpgl/downloadAttachFile.action?Id="+Id+"&attachName="+encodeURI(encodeURI(attachName))); 
    }
    });
    
    //action 后台一些主要的包和类
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    
    /**
    * 下载文件
    * @return
    * @throws UnsupportedEncodingException 
    */
    
    @Action(value="downloadAttachFile")
    public String downloadAttachFile(){
      //从前台传过来的附件名字:格式是,“2006#系统测试.docx格式"
      String downloadName =wpyId+"#"+attachName;
      //传入的数据
      System.out.println(downloadName);
      try {
        OutputStream os = this.response.getOutputStream();
    
              //OutputStream os = this.getResponse().getOutputStream();
    
        if (Util.isIE(this.request)) //判断客户端是否为IE
        {
    
                   //编码
          downloadName = URLEncoder.encode(downloadName, "UTF-8");
        } 
        else 
        {
          downloadName = new String(downloadName.getBytes("UTF-8"),"iso-8859-1");
        }
        //设置让浏览器弹出下载对话框的Header
        this.response.setContentType("application/x-download");
        this.response.addHeader("Content-Disposition","attachment;filename="" + downloadName + """);
        this.response.flushBuffer();
        //将前台的数据经过包装后(加了头数据,并经过编码)需要解码
        String downloadName1 =URLDecoder.decode(downloadName, "UTF-8");
        System.out.println(downloadName1);
        //解码的结果输出
        String bgFile=realPath + File.separator + downloadName1; 
    
        FileInputStream fis = new FileInputStream(bgFile);
        Util.copyStrem(fis, os);
        fis.close();
        os.close();
        System.out.println("==========成功了!!===========");
      } catch (IOException e) {
    
      e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
      System.out.println("==========出错了!!===========");
      }
    
      return NONE;
    }
  • 相关阅读:
    将博客搬至CSDN
    规范化设计的范式的个人理解
    Codeforces Round #422 (Div. 2) D题要补的知识点
    Codeforces第一次rated比赛
    Codeforces 818B Permutation Game
    USACO Dynamic Programming (1)
    关于数据库中除法的一个小问题
    USACO hamming
    USACO Healthy Holsteins
    USACO Sorting a Three-Valued Sequence
  • 原文地址:https://www.cnblogs.com/Lxiaojiang/p/6092899.html
Copyright © 2020-2023  润新知