zoukankan      html  css  js  c++  java
  • 模板下载

    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;
        }
  • 相关阅读:
    电信的星空极速客户端软件强制安装策略升级了
    CSS 控件适配器工具包对事件处理的 Bug 以及修正办法
    IronPython 源码剖析系列(1):IronPython 编译器
    对 CSS 控件适配器处理事件的 Bug 进一步修正
    明天去长春出差。。。
    Windows 系统右键菜单假死问题解决一例
    使用 Anthem.NET 的常见回调(Callback)处理方式小结
    TreeViewVisitor: 一个快捷访问 TreeView 控件节点的帮助类
    ASP.NET Web开发框架之六 数据库文档方法,工具和实践
    推荐功能强大的ORM Profiler,ORM问题追踪的利器
  • 原文地址:https://www.cnblogs.com/acme6/p/9237079.html
Copyright © 2011-2022 走看看