zoukankan      html  css  js  c++  java
  • excel导出

      前端代码:

    <form id="downloadForm" action="#" method="get">
       <input type="hidden" id="fileId" name="fileId">
        <input type="hidden" id="filename" name="filename">
    </form>

    JS:

     var filename = encodeURI(fksm + ".xls")
            $("#downloadForm #fileId").val(fileId);
            $("#downloadForm #filename").val(filename);
            $('#downloadForm').attr('action', '${pageContext.request.contextPath}/cs/downPaymentFile.do?fileId=' + fileId + '&filename=' + filename);
            $('#downloadForm').submit();

    后台代码:

    /**
         * 下载付款附件
         * @param request
         * @param response
         */
        @RequestMapping(value = "/downPaymentFile.do", method = RequestMethod.GET)
        public void downPicture(HttpServletRequest request, HttpServletResponse response) throws Exception{
            InputStream in = null;
            String fileId = request.getParameter("fileId");
            String filename = request.getParameter("filename");
            filename = new String(filename.getBytes("GB2312"),"iso-8859-1");
    
            try {
                String messageRes = ImageSystemUtils.fileView(fileId);
                JSONObject msgGetPictures = JSONObject.parseObject(messageRes);
                String statusGetPictures = msgGetPictures.getString("status");
                //若返回成功
                if (("200").equals(statusGetPictures)) {
                    //获取返回的data字段值
                    String data = msgGetPictures.getString("data");
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    String url = jsonObject.getString("filePath").replaceAll("\\", "/");
                    
                    URL httpUrl = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.connect();
    
                    in = conn.getInputStream();
                    byte[] bs = new byte[1024 * 10];
                    int len = 0;
                    response.reset();
    
                    response.setHeader("Pragma", "no-cache");
                    response.setHeader("Cache-Control", "no-cache");
                    response.setHeader("Content-Type", "application/octet-stream");
                    response.setHeader("Content-Disposition", "attachment;filename=" + filename);
                    ServletOutputStream out = response.getOutputStream();
                    while ((len = in.read(bs)) != -1) {
                        out.write(bs, 0, len);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException("下载失败");
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
        }
  • 相关阅读:
    进程详解(1)——可能是最深入浅出的进程学习笔记
    贪吃蛇游戏C语言源代码学习
    经典功率谱估计及Matlab仿真
    CAN总线(一)
    实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1
    Linux驱动开发概述
    assert的用法
    基于WDF的PCI/PCIe接口卡Windows驱动程序(4)- 驱动程序代码(源文件)
    基于WDF的PCI/PCIe接口卡Windows驱动程序(3)- 驱动程序代码(头文件)
    C语言中的union
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/11724986.html
Copyright © 2011-2022 走看看