spring webflux系列之文件流下载文件(不用本地创建文件直接文件流下载)
从上一个spring webflux系列上传文件(多)继承到七牛云篇
internet上的基本教程是创建本地文件,然后下载并删除本地文件
这里使用的是流下载文件,即inputStream和outputStream
是直接粘贴代码吗(简易版) )。
@ get mapping (value=(/downloadfile ) ) publicmonovoiddownloadtest (serverhttpresponseresponse ) )文件文件=新文件) //这里为了模拟流,去读本地文件inputstream in=null; try { in=new file inputstream (文件); fluxdatabufferdatabufferflux=databufferutils.readbyte channel (in :3360 get channel,new DefaultDataBufferFactory ),zerer httpheadersheaders=zerocopyhttpoutputmessage.get headers (; headers.set (http headers.content _ disposition,' attachment; filename=test.xlsx '; //vnd.ms-excel是octet-streammediatypeapplication=new media type (' application ',' vnd.ms-excel ',charset.) returnzerocopyhttpoutputmessage.write with (databufferflux; }catch(exceptione ) { e.printStackTrace ); } return Mono.empty (; }简易版本自己稍微修改一下就可以使用了
这里简单地表示您想在inputstream的outputstream中直接更改为outputstream
in:getChannel是创建流通道(管道)
DefaultDataBufferFactory是默认工厂,您也可以自行定制处理厂
4096后面有读取大小
然后继续更新spring webflux系列
集成到项目的开源github中