博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
extjs 下载文件 关键前后端代码
阅读量:4589 次
发布时间:2019-06-09

本文共 2972 字,大约阅读时间需要 9 分钟。

 

//前端关键代码

var qdGridPanel = new Ext.grid.GridPanel({

    title : '<span class="commoncss">【文件发布明细】</span>',
    region : 'east',
    store : qdGridStore,
    cm : qdGridColumns,
    height : 300,
    width : 620,
    stripeRows : true,
    trackMouseOver : true,
    loadMask : true,
    frame : true,
    clicksToEdit : 1,
    listeners: {
    cellclick: function(a, b, c, e) {   //点击某个grid的单元格 触发该事件    
      var record = a.getStore().getAt(b);   //Get the Record    获取某行元素
      var filepath = 'http://202.91.245.28:25808/pdf/3305/20190112/33052190211875_trainRecord.pdf';  //record.data.filepath    获取要下载的http文件路径
      if(c=='5'){

                             //关键代码

        var downloadIframe = document.createElement('iframe');
        downloadIframe.src = '../../../dfo/biz_web/oper/EquUpLoadManager/loadfile.do?filepath='+filepath;
        downloadIframe.style.display = "none";
        document.body.appendChild(downloadIframe);

//注意   谷歌浏览器不会弹出下载框,直接会下载到默认路径

      });

      // window.open('../../../dfo/biz_web/oper/EquUpLoadManager/loadfile.do?filepath='+filepath);

    }else{

      return ;
    }

    // win(); //创建一个win窗口

    }
  }
});

 

//服务端代码

 

  if(!"".equals(filepath)&&filepath!=null){

    //根据条件得到文件路径
    System.out.println("===========文件路径==========="+filepath);
    url = new URL(filepath);
    httpUrl = (HttpURLConnection) url.openConnection();  //链接 http路径的资源文件
    //链接指定的资源
    httpUrl.connect();
    // 获取网络输入流
    InputStream inStream = httpUrl.getInputStream();    //获取资源文件的流
    //将文件读入文件流(相对路径)
    //InputStream inStream = this.getServlet().getServletContext().getResourceAsStream("/uploaddata/template/AutoTemplate.xls");---测试 本地文件使用

    //获得浏览器代理信息

    final String userAgent = request.getHeader("USER-AGENT");
    //判断浏览器代理并分别设置响应给浏览器的编码格式
    String finalFileName = filepath.substring(filepath.lastIndexOf("/")+1);
    if(userAgent.contains("MSIE")||userAgent.contains("Trident")){//IE浏览器
      finalFileName = URLEncoder.encode(finalFileName,"UTF8");
      System.out.println("IE浏览器");
    }else if(userAgent.contains("Mozilla")){//google,火狐浏览器
      finalFileName = new String(finalFileName.getBytes(), "ISO8859-1");
    }else{
      finalFileName = URLEncoder.encode(finalFileName,"UTF8");//其他浏览器
    }
    //设置HTTP响应头
    response.reset();//重置 响应头
    //response.setContentType("application/x-download");//告知浏览器下载文件,而不是直接打开,浏览器默认为打开
    response.setHeader("Content-type", "application-download");
    response.addHeader("Content-Disposition" ,"attachment;filename=\"" +finalFileName+ "\"");//下载文件的名称
    response.setCharacterEncoding("utf-8");
    //response.setContentType("multipart/octet-stream");x-msdownload
    response.setContentType("multipart/x-msdownload");
    // 循环取出流中的数据
    byte[] b = new byte[1024];
    int len;
    while ((len = inStream.read(b)) > 0){
      response.getOutputStream().write(b, 0, len);
    }
    response.getOutputStream().flush();
    inStream.close();
    response.getOutputStream().close();

 

 

 

 

转载于:https://www.cnblogs.com/xplj2013/p/10436150.html

你可能感兴趣的文章
1282: 排列计数 perm
查看>>
牛客小白月赛15 C 表单 ( map 使用)
查看>>
oracle中的索引
查看>>
STM8S——Analog/digital converter (ADC)
查看>>
LeetCode-211 Add and Search Word - Data structure design
查看>>
jquery each遍历节点使用
查看>>
sql笔记 获取指定数据库下的所有表
查看>>
第一个定时脚本--nginx日志的切割
查看>>
计算机科学导论---算法
查看>>
Swift补充:swift调用oc单例类方法
查看>>
总结一下vue调试的方法
查看>>
WinCE Overlay - 示例:mosquito
查看>>
Spring Boot Starter列表
查看>>
Linux中的#和$区别
查看>>
win8设置自动关机
查看>>
什么是java序列化,如何实现java序列化?
查看>>
创建动态节点和动态Table
查看>>
软工实践第三次结对作业——原型设计
查看>>
机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价
查看>>
以太坊2.0原理详解 - 开篇(一)
查看>>