当前位置: 首页 > news >正文

springboot单文件,多文件下载方式

简单大文件下载:

/*** 下载大文件* @param path 路径* @param fileName 文件名* @return* @throws IOException*/
public static ResponseEntity<InputStreamResource> downloadFile(String path, String fileName) throws IOException {Path filePath = Paths.get(path);long size = Files.size(filePath);InputStreamResource resource = new InputStreamResource(Files.newInputStream(filePath));HttpHeaders headers = new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, STR."attachment; filename=\{URLEncoder.encode(fileName, StandardCharsets.UTF_8)}");headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);headers.add(HttpHeaders.ACCEPT_RANGES, "bytes");return ResponseEntity.ok().headers(headers).contentLength(size).body(resource);
}

多文件下载:

/*** 起线程压缩文件,返回流* @param pathList 路径集合* @param fileName 文件名* @return*/
public static ResponseEntity<InputStreamResource> zipByThread(List<String> pathList, String fileName) throws IOException {PipedInputStream pipedInputStream = new PipedInputStream();PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);// 启动新线程以写入管道输出流Thread.ofVirtual().start(() -> {try (ZipOutputStream zos = new ZipOutputStream(pipedOutputStream)) {for (String path : pathList) {Path filePath = Paths.get(path);if (Files.exists(filePath)) {if (Files.isDirectory(filePath)) {zipDirectory(filePath, filePath.getFileName().toString(), zos);} else {zipFile(filePath, zos);}}}} catch (IOException e) {log.error("文件压缩失败:{}", e.getMessage());throw new BusinessException("文件压缩失败");} finally {try {pipedOutputStream.close();} catch (IOException _) {}}});HttpHeaders headers = new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, STR."attachment; filename=\{URLEncoder.encode(fileName, StandardCharsets.UTF_8)}");headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(-1));return ResponseEntity.ok().headers(headers).body(new InputStreamResource(pipedInputStream));
}/*** 压缩文件夹* @param folder* @param parentFolder* @param zos* @throws IOException*/
private static void zipDirectory(Path folder, String parentFolder, ZipOutputStream zos) throws IOException {try (Stream<Path> paths = Files.walk(folder)) {paths.filter(Files::isRegularFile).forEach(path -> {String zipEntryName = Paths.get(parentFolder).resolve(folder.relativize(path)).toString().replace("\\", "/");try {zos.putNextEntry(new ZipEntry(zipEntryName));Files.copy(path, zos);zos.closeEntry();} catch (IOException e) {throw new BusinessException("压缩文件夹失败");}});}
}/*** 压缩文件* @param file* @param zos* @throws IOException*/
private static void zipFile(Path file, ZipOutputStream zos) throws IOException {zos.putNextEntry(new ZipEntry(file.getFileName().toString()));Files.copy(file, zos);zos.closeEntry();
}

注意

如果使用了nginx反代,下载超时需要在nginx配置

proxy_buffering off;
http://www.lryc.cn/news/461607.html

相关文章:

  • JIT详解
  • 线下陪玩导游系统软件源码,家政预约服务源码(h5+小程序+app)
  • 模拟退火算法最常见知识点详解与原理简介控制策略
  • C语言高效内存管理:对齐、缓存与位域
  • ES操作指南
  • 【黑苹果】记录MacOS升级Sonoma的过程
  • 向“新”发力,朝“质”攀峰 | 资福医疗携手大圣胃肠一体内窥镜系统亮相江苏省医学会第八次健康管理学学术会议
  • springboot项目多个数据源配置 dblink
  • leetcode中哈希的python解法:Counter()介绍
  • VAS1800Q奇力科技线性芯片电荷泵热处理AEC-Q1000
  • Java 枚举的 valueOf() 方法与 Stream API 查找枚举对象
  • Git的认识及基本操作
  • python 日志库loguru
  • 基于SpringBoot+Vue+uniapp的在线招聘平台的详细设计和实现
  • Chrome谷歌浏览器加载ActiveX控件之JT2Go控件
  • Java基础概览和常用知识(七)
  • STL-string
  • 数据库基础-学习版
  • 【Gin】Gin框架介绍和使用
  • AI大模型带来哪些创业机遇?
  • [Linux] 层层深入理解文件系统——(3)磁盘组织存储的文件
  • Apache Cordova学习计划
  • Unity学习日志-API
  • Java基础常见面试题总结(上)
  • 4 -《本地部署开源大模型》在Ubuntu 22.04系统下部署运行ChatGLM3-6B模型
  • 本地如何使用Pycharm连接远程服务器调试torchrun
  • Visual Studio 2022常用快捷键
  • mysql innodb 引擎如何直接复制数据库文件?
  • python中的global和nonlocal关键字以及闭包和模块
  • LabVIEW风机滚动轴承监测系统