spring webflux系列之文件流下载文件

2 篇文章 0 订阅
1 篇文章 0 订阅

spring webflux系列之文件流下载文件(不用本地创建文件直接文件流下载)
继承上一篇spring webflux系列之上传文件(多)到七牛云篇
网上基本上全部教程都是创建本地文件 才去下载 又要删除本地文件
这里使用到流下载文件 也就是inputStream 和 outputStream 都行
还是直接贴代码(简易版本)

   @GetMapping(value = "/downloadFile")
    public Mono<Void> downloadTest(ServerHttpResponse response) {
        File file = new File("C:\\Users\\zjian\\Desktop\\cache\\test.xlsx");
        //这里为了模拟流 而去读取本地文件
        FileInputStream in =null;
        try {
            in = new FileInputStream(file);
            Flux<DataBuffer> dataBufferFlux = DataBufferUtils.readByteChannel(in::getChannel,new DefaultDataBufferFactory(),4096);
            ZeroCopyHttpOutputMessage zeroCopyHttpOutputMessage = (ZeroCopyHttpOutputMessage)response;
            HttpHeaders headers = zeroCopyHttpOutputMessage.getHeaders();
            headers.set(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=test.xlsx");
            //vnd.ms-excel 可以替换为 octet-stream
            MediaType application = new MediaType("application", "vnd.ms-excel", Charset.forName("UTF-8"));
            headers.setContentType(application);
            return zeroCopyHttpOutputMessage.writeWith(dataBufferFlux);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  Mono.empty();
    }

简易版本 自己稍加修改就能使用
这里简单的展示inputstream的 想用outputstream直接改成outputstream就行
in::getChannel 是创建流通道(管道)
DefaultDataBufferFactory是默认工厂 你也可以自己自定义处理工厂
4096后面是读取size

后续我会继续更新spring webflux 系列
并整合成项目开源 github开源地址

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring WebFluxSpring Framework的一个模块,它提供了一种响应式编程的方式来构建基于事件驱动的应用程序。它的执行过程与Spring MVC的DispatchServlet相似,但核心控制器是DispatchHandler,实现了WebHandler接口。要使用Spring WebFlux,首先需要添加相关的依赖,例如在pom.xml文件中添加spring-boot-starter-webflux依赖。接着,在代码中创建一个RestController,并使用@RequestMapping注解指定URL路径,然后在方法中使用@GetMapping注解指定处理GET请求的方法,并返回一个Mono对象。在主类中使用@SpringBootApplication注解标记应用程序入口,并在main方法中使用SpringApplication.run方法启动应用程序。最后,使用curl命令或浏览器访问http://localhost:8080/webflux/hello可以得到"Hello Spring Webflux"的响应。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springWebflux](https://blog.csdn.net/weixin_44895829/article/details/123509954)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [一文弄懂 Spring WebFlux 的来龙去脉](https://blog.csdn.net/m0_38017860/article/details/126590395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Spring学习(六):WebFlux](https://blog.csdn.net/a1275302036/article/details/116492120)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值