SpringBoot实现接口提供下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ResponseEntity<FileSystemResource> export(File file) {
    if (file == null) {
        return null;
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".xls");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");
    headers.add("Last-Modified", new Date().toString());
    headers.add("ETag", String.valueOf(System.currentTimeMillis()));
 
    return ResponseEntity
            .ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.parseMediaType("application/octet-stream"))
            .body(new FileSystemResource(file));
}
@RequestMapping("/getExcel")
    public ResponseEntity<FileSystemResource> getUserExcel(@RequestParam(required=false) String fromId,@RequestParam(required=false) String type) throws Throwable {
        logger.debug("获取用户excel参数:fromId=",fromId);
        Children children = depthFindParentId(fromId, new Children());
        File file = getExcelFile(children);
        return export(file);
    }
    

亲测可用

加一个递归函数:

复制代码
public Children depthFindParentId( String parentId,Children child) throws Throwable {
        Children children = _ucApi.findChildren( parentId, true, true, true, null);
        child.getUsers().addAll(children.getUsers()); //用户
        child.getDepartments().addAll(children.getDepartments());//部门
        if (children.getDepartments().size() > 0) {
            for (Department dep : children.getDepartments()) {
                depthFindParentId(dep.getId(),child);
            }
        }
        return child;
    }
复制代码

 文件不落地

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@RequestMapping("/excelExample")
    public ResponseEntity<byte[]> excel() throws IOException {
        // 创建工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建用户工作表
        HSSFSheet sheet = workbook.createSheet("用户列表");
        HSSFRow rows = sheet.createRow(0);
        rows.createCell(0).setCellValue("用户ID");
        rows.createCell(1).setCellValue("姓名");
        rows.createCell(2).setCellValue("手机号");
        rows = sheet.createRow(0);
        rows.createCell(0).setCellValue("栏目ID");
        rows.createCell(1).setCellValue("栏目名称");
 
        sheet = workbook.createSheet("部门列表");
        rows = sheet.createRow(0);
        rows.createCell(0).setCellValue("部门ID");
        rows.createCell(1).setCellValue("部门名称");
 
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
        HttpHeaders httpHeaders = new HttpHeaders();
        String fileName = new String("用户部门.xls".getBytes("UTF-8"), "iso-8859-1");
        httpHeaders.setContentDispositionFormData("attachment", fileName);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(outputStream.toByteArray(), httpHeaders, HttpStatus.CREATED);
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
        return filebyte;
    }

  

posted @   凉城  阅读(8819)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示