springboot 下载 Excel 文件的 Controller 层案例

环境 pom.xml 中 springboot版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.15</version>
        
    </parent>

Excel 文件依赖:


<!-- POI -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>ooxml-schemas</artifactId>
			<version>1.1</version>
		</dependency>

demo1

    // 下载 Excel
    @GetMapping(value = "/downloadData")
    public ResponseEntity<byte[]> downloadData2(
            @RequestParam("id") String id) throws IOException {

        log.info("---------------downloadData start-------------");

        String rootDirectory = request.getServletContext().getRealPath("/");
        log.info("rootDirectory:"+rootDirectory);

        // 临时放置文件目录
        Path p = Paths.get(rootDirectory, "temp");

        if (!p.toFile().exists()) {
            p.toFile().mkdirs();
        }
        log.info("---------------downloadData start 2 Render.... -------------");

        // 写 业务逻辑,生成 Excel File
        File file = null; 
        

        log.info("###------------the excel file address:"+file.getAbsolutePath());

        // 下载成功返回二进制流
        return getResponseEntity(file,null,false);

    }


    private static ResponseEntity<byte[]> getResponseEntity(File file,String userAgent,boolean inline) throws IOException {

        ResponseEntity.BodyBuilder bodyBuilder = ResponseEntity.ok();
        bodyBuilder.contentLength(file.length());
        // 二进制数据流
//        bodyBuilder.contentType(MediaType.APPLICATION_OCTET_STREAM);

        // 文件名编码

        String encodeFileName = URLEncoder.encode(file.getName(), "UTF-8");
        // IE
        if (userAgent!=null&&userAgent.indexOf("MSIE") > 0) {
            bodyBuilder.header("Content-Disposition", "attachment;filename=" + encodeFileName);
        } else { // 其他浏览器

            if (inline) {// 在浏览器中直接打开

                URL url = new URL("file:///" + file);
                bodyBuilder.header("Content-Type", url.openConnection().getContentType());
                bodyBuilder.header("Content-Disposition", "inline;filename*=" + encodeFileName);
            } else {
                // 直接下载

                bodyBuilder.header("Content-Disposition", "attachment;filename*=" + encodeFileName);
                bodyBuilder.header("Content-Type","application/vnd.ms-excel;charset=utf8");
            }


        }
        // 下载成功返回二进制流
        return bodyBuilder.body(Files.readAllBytes(file.toPath()));
    }

demo2

    @GetMapping(value = "/download2")
    public void download2(@RequestParam("id") String id,
                              
                                  @RequestParam(required = false, defaultValue = "false") @ApiParam("参数 inline表示是否要在线浏览,true是,false否(默认).") boolean inline,

                                  @RequestHeader("user-agent") @ApiParam("参数userAgent 是为了兼容IE判断,如果使用谷歌,火狐浏览器就可以省略这个参数.") String userAgent,
                                  ServletRequest request,
                                  HttpServletResponse response) throws IOException {

        String rootDirectory = request.getServletContext().getRealPath("/");
//        System.out.println("rootDirectory:"+rootDirectory);

        // 临时放置文件目录
        Path p = Paths.get(rootDirectory, "temp");

        if (!p.toFile().exists()) {
            p.toFile().mkdirs();
        }


        // 根据业务生成 文件
        File file = new File("");

        this.download(response,file,null);

    }

    private void download(HttpServletResponse response, File file, String fileName){
        if(fileName==null){
            fileName = file.getName();
        }

        try(FileInputStream is = new FileInputStream(file);
            OutputStream os = response.getOutputStream()) {
            //通用的mime类型
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));



                byte[] buf = new byte[1024];
                int len = 0;
                while((len = is.read(buf))!=-1)
                {
                    os.write(buf,0,len);
                }

        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

相关推荐

  1. springboot 下载 Excel 文件 Controller 案例

    2024-03-10 18:04:03       22 阅读
  2. Springboot项目中Controller单元测试

    2024-03-10 18:04:03       32 阅读
  3. OSS 文件下载-Excel

    2024-03-10 18:04:03       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 18:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 18:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 18:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 18:04:03       20 阅读

热门阅读

  1. AI辅助研发,引领科技新潮流

    2024-03-10 18:04:03       27 阅读
  2. C++核心编程

    2024-03-10 18:04:03       19 阅读
  3. 力扣背包问题

    2024-03-10 18:04:03       24 阅读
  4. 【微软技术】介绍

    2024-03-10 18:04:03       24 阅读
  5. 面试题之——SpringBoot的好处?

    2024-03-10 18:04:03       25 阅读
  6. django 的 filter 使用技巧

    2024-03-10 18:04:03       23 阅读