zoukankan      html  css  js  c++  java
  • extjs 下载文件 关键前后端代码

    //前端关键代码

    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();

     

     

  • 相关阅读:
    Tensorflow-卷积神经网络CNN
    Tensorflow-tensorboard展示网络结构
    机器学习十讲-第二讲回归
    Tensorflow-各种优化器总结与比较
    Tensorflow-交叉熵&过拟合
    机器学习十讲-第一讲
    Tensorflow-线性回归与手写数字分类
    Tensorflow-基础使用
    Python-Flask搭建Web项目
    mybatis-plus之逻辑删除&自动填充&乐观锁
  • 原文地址:https://www.cnblogs.com/xplj2013/p/10436150.html
Copyright © 2011-2022 走看看