码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot之下载excel模板

时间:2019-09-25 15:46:20      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:encode   pos   xls   下载   tchar   fileinput   coding   getpath   form   

@GetMapping(value = "/downloadTemplate")
    public void downFrozenTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = "模板"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx";
        ServletOutputStream out;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("content-type:octet-stream");
        //文件在项目中的存放路径
        String filePath = getClass().getResource("/templates/template1.xlsx").getPath();
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理:
            fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1");
        }
        filePath = URLDecoder.decode(filePath, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        FileInputStream inputStream = new FileInputStream(filePath);
        out = response.getOutputStream();
        int b = 0;
        byte[] buffer = new byte[1024];
        while ((b = inputStream.read(buffer)) != -1) {
            //写到输出流(out)中
            out.write(buffer, 0, b);
        }
        inputStream.close();
        if (out != null) {
            out.flush();
            out.close();
        }
    }

 

SpringBoot之下载excel模板

标签:encode   pos   xls   下载   tchar   fileinput   coding   getpath   form   

原文地址:https://www.cnblogs.com/zengnansheng/p/11584523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!