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

spring webflux文件上传与下载

1、文件上传:

Controller:

 @PostMapping("/import")public void importImage(@RequestPart("file") FilePart filePart) {imageService.importImage(filePart);}

Service:

   public void importImage(FilePart filePart) {File saveFile = new File("/tmp/a.txt");if (!copyFile(filePart, saveFile)) {throw new OpsManagerException("文件保存失败!");}}
public boolean copyFile(FilePart filePart, File tempFile) {try {if (tempFile.exists()) {tempFile.delete();}tempFile.createNewFile();AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE);AtomicBoolean copyFinish = new AtomicBoolean(false);DataBufferUtils.write(filePart.content(), channel, 0).doOnComplete(() -> copyFinish.set(true)).subscribe();AtomicInteger atomicInteger = new AtomicInteger(0);while (!copyFinish.get()) {Thread.sleep(5000);atomicInteger.addAndGet(1);if (atomicInteger.get() > 720) {throw new OpsManagerException("文件过大");}}return true;} catch (Exception e) {log.error("save upload file error", e);return false;}}

2、文件下载:

Controller:

  @GetMapping("/export")public Mono<Void> downloadFile(ServerHttpResponse response) {File file = new File("/tmp/workplace.zip");return TaskUtils.downloadFile(file, response);}

Service:

public static Mono<Void> downloadFile(File file, ServerHttpResponse response) {try {response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);response.getHeaders().set(HttpHeaders.CONTENT_LENGTH, "" + file.length());Flux<DataBuffer> dataBufferFlux = Flux.create((FluxSink<DataBuffer> emitter) -> {InputStream in = null;try {in = new FileInputStream(file);byte[] buffer = new byte[1024 * 1024]; // Adjust buffer size based on your needswhile (in.read(buffer) != -1) {emitter.next(response.bufferFactory().wrap(buffer));}emitter.complete();} catch (IOException e) {emitter.error(e);} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}});return response.writeAndFlushWith(Mono.just(dataBufferFlux));} catch (IOException ex) {logger.error("download file error!", ex);throw new RuntimeException("download file error!");}}
http://www.lryc.cn/news/253515.html

相关文章:

  • Android MVVM+coroutine+retrofit+flow+hilt
  • elasticsearch副本和分片
  • 【Python】zip
  • 西安安泰——ATA-1220E宽带放大器
  • 数据结构和算法专题---4、限流算法与应用
  • 亚信安慧AntDB受邀分享核心业务系统全域数据库替换实践
  • 1.uniapp基础
  • typescript中的策略模式
  • Hadoop学习笔记(HDP)-Part.16 安装HBase
  • C语言练习记录(蓝桥杯练习)(小蓝数点)
  • RPG项目01_层级设置
  • 相关基础知识
  • 基于单片机的智能健康监测手环的设计
  • boost-字符串处理-判断-查找-裁剪-删除-替换-分割-合并
  • Django 开发 web 后端,好用过 SpringBoot ?
  • 【矩阵】54.螺旋矩阵(顺时针打印矩形元素)
  • 【数据中台】开源项目(5)-Amoro
  • _WorldSpaceLightPos0的含义 UNITY SHADER
  • iOS不越狱自动挂机
  • 智能优化算法应用:基于鼠群算法无线传感器网络(WSN)覆盖优化 - 附代码
  • FL Studio中如何录音的技巧,让你的声音更加出众哦!
  • 前端React基础面试题
  • 【1day】致远A6系统任意文件下载漏洞学习
  • 朝花夕拾华山平台流水账
  • 云原生周刊:K8s 的 YAML 技巧 | 2023.12.4
  • Leetcode.2477 到达首都的最少油耗
  • sizeof()、strlen()、length()、size()的区别(笔记)
  • Redis击穿(热点key失效)
  • 分类预测 | Matlab实现OOA-CNN-SVM鱼鹰算法优化卷积支持向量机分类预测
  • class文件结构