下载resource下的excel文件

1、将excel文件放项目resources目录下

2、打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况
 <configuration>
   <encoding>UTF-8</encoding>
   <nonFilteredFileExtensions>
   <nonFilteredFileExtension>xls</nonFilteredFileExtension>
   <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
   </nonFilteredFileExtensions>
</configuration>
3、resource配置
<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.xlsx</include>
                </includes>
            </resource>
        </resources>
4、读取resources下的xlsx文件
 try {
            //获取模板文件
            File sourceFile = ResourceUtils.getFile("classpath:bankNameTemplate.xlsx");
            //转换为文件流
            BufferedInputStream fs = new BufferedInputStream(new FileInputStream(sourceFile));             
            //指定默认下载名称
            String fileName = "XXX.xlsx";
            //配置response的头
            response.reset();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            try {
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //循环从文件中读出数据后写出,完成下载
            byte[] b = new byte[1024];
            int len;
            while ((len = fs.read(b)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
            fs.close();
        } catch (Exception e) {
            logger.error("下载Excel模板异常", e);
        }
5.前端请求
  window.location.href = "/exportExcelTemplate";  
原文地址:https://www.cnblogs.com/lanqingyu/p/12017715.html