下载工程resources目录下的模板excel文件

一、添加依赖

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>

二、编写接口

@GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request,
                                 HttpServletResponse response){
        String fileName = "template"+File.separator+"字段标准导入模板.xlsx";
        InputStream is = ClassLoader.getSystemResourceAsStream(fileName);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        Workbook wb = null;
        try {
            wb = new XSSFWorkbook(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            wb.write(response.getOutputStream());
        } catch (Exception e) {
            log.error("下载导入模板异常{}", e.getMessage());
        } finally {
            IOUtils.closeQuietly(wb);
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


三、这种代码,在windows上调试没问题,打成jar包发到linux环境上会有问题,改成如下代码就行:

@GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request,
                                 HttpServletResponse response){
        String fileName = "template"+File.separator+"ColumnStandardImportTemplate.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        try (InputStream is = new ClassPathResource(fileName).getInputStream();
             Workbook wb = new XSSFWorkbook(is);
             ServletOutputStream os = response.getOutputStream()) {
            wb.write(os);
            os.flush();
        } catch (Exception e) {
            log.error("下载导入模板异常", e);
        }
    }


    

相关推荐

  1. 下载工程resources目录模板excel文件

    2024-06-19 07:24:05       8 阅读
  2. Springboot resource excel

    2024-06-19 07:24:05       35 阅读
  3. [Spark] 读取项目resources/文件

    2024-06-19 07:24:05       38 阅读
  4. 解决SpringBoot jar包resources目录文件读取不到

    2024-06-19 07:24:05       39 阅读
  5. 处理合并目录Excel文件数据并指定列去重

    2024-06-19 07:24:05       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-19 07:24:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-19 07:24:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-19 07:24:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-19 07:24:05       18 阅读

热门阅读

  1. 架构设计 - Nginx Proxy Cache 缓存配置

    2024-06-19 07:24:05       5 阅读
  2. 认识QML

    2024-06-19 07:24:05       5 阅读
  3. CSS 修改鼠标图标样式

    2024-06-19 07:24:05       5 阅读
  4. Linux中常用的压缩与解压文件

    2024-06-19 07:24:05       5 阅读
  5. Eigen中的array() square() asDiagonal()

    2024-06-19 07:24:05       5 阅读