• 模板下载


    function downloadExcelTemplate_Click(){
         var url = "downloadExcelTemplate.do?isdebug=true";
         location.href = url;
    }
    

      

    /**
         * 下载导入模板
         *
         * @param request
         * @param response
         * @return
         */
        @RequestMapping(value = "/downloadExcelTemplate.do", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
        @ResponseBody
        public ResponseModel downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) {
            ResponseModel responseModel = new ResponseModel();
            responseModel.setStatusCode("fail");
            String header = request.getHeader("User-Agent").toUpperCase();
            String fileName = "z中文";
            try {
                if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
                    fileName = URLEncoder.encode(fileName, "utf-8");
                    fileName = fileName.replace("+", "%20");    //IE下载文件名空格变+号问题
                }
            
                String filepath = request.getSession().getServletContext().getRealPath("/") +"excel/excel_route.xlsx";
                InputStream is = new FileInputStream(filepath);
                OutputStream os = setRespone(fileName, response);
                int length = 1024;
                int readLength = 0;
                byte buf[] = new byte[1024];
                readLength = is.read(buf, 0, length);
                while (readLength != -1) {
                    os.write(buf, 0, readLength);
                    readLength = is.read(buf, 0, length);
                }
                os.flush();
                os.close();
                responseModel.setStatusCode("success");
            } catch (UnsupportedEncodingException e) {
                getLogger().error("AccountInputController.downloadExcelTemplate method UnsupportedEncodingException  :" + e.getMessage(), e);
            } catch (FileNotFoundException e) {
                getLogger().error("AccountInputController.downloadExcelTemplate method FileNotFoundException  :" + e.getMessage(), e);
            } catch (Exception e) {
                getLogger().error("AccountInputController.downloadExcelTemplate method Exception :" + e.getMessage(), e);
            }
            return responseModel;
        }
    /**
         * 设置输出响应下载流
         *
         * @param fileName
         * @param response
         * @return
         * @throws IOException
         */
        private OutputStream setRespone(String fileName, HttpServletResponse response) throws IOException {
            //输出流
            OutputStream os = response.getOutputStream();
            //重置输出流
            response.reset();
    
            response.setContentType("application/vnd.ms-excel");
            //设置响应标题>这里浏览器会提示用户选择下载文件需要存放的路径>
            //后续生成的文件在输出流关闭后>action返回detailExcel进result指定响应内容为excel>自动写入该excel到该用户指定的路径中
            response.setHeader("Content-disposition", "attachment; fileName=" + new String((fileName + ".xlsx").getBytes(), "iso8859-1"));
            //response.setHeader("Content-disposition", "attachment; fileName=" + URLEncoder.encode(brow,"utf-8")+".xls");(不可用)
            return os;
        }
  • 相关阅读:
    ES6之模块化
    ES6之展开运算符
    ES6之解构赋值
    ES6之对象的语法糖
    ES6之函数的语法糖
    ES6之模板字符串
    Exchanger详解
    DNS解析过程
    CyclicBarrier详解
    ConuntDownLatch详解
  • 原文地址:https://www.cnblogs.com/acme6/p/9237079.html
Copyright © 2020-2023  润新知