淮安淮阴网站建设,网站开发环境包括什么,网站运营服务商,网上怎么申请个人营业执照学习链接
SpringBootvue文件上传下载预览大文件分片上传文件上传进度
Blob File FileReader ArrayBuffer
VueSpringBoot实现文件的分片下载
video标签学习 xgplayer视频播放器分段播放mp4#xff08;Range请求交互过程可以参…学习链接
SpringBootvue文件上传下载预览大文件分片上传文件上传进度
Blob File FileReader ArrayBuffer
VueSpringBoot实现文件的分片下载
video标签学习 xgplayer视频播放器分段播放mp4Range请求交互过程可以参考这个里面的截图
【java】java实现大文件的分片上传与下载springbootvue3)代码已fork至本地
代码
FileController
这里面的代码实现完全可以参考ResourceHttpRequestHandler#handleRequest
RestController
public class FileController {private static final int BUFFER_SIZE 4 * 1024;RequestMapping(path chunkdownload, method {RequestMethod.HEAD, RequestMethod.POST})public void chunkdownload(HttpServletRequest request, HttpServletResponse response) throws Exception {File file new File(D:/usr/test/demo.mp4);// 文件总大小long fileSize file.length();// 设置 Content-Type 和 相关响应头// (这里分片下载响应头设置, 其实可以参考ResourceHttpRequestHandler#handleRequest,// 和 video标签学习 xgplayer视频播放器分段播放mp4 - https://blog.csdn.net/qq_16992475/article/details/130945997)response.setContentType(application/octect-stream;charsetUTF-8);response.setHeader(Content-Disposition, attachment; filename\ file.getName() \);response.setHeader(Accept-Ranges, bytes);// 检查请求头中是否有Range请求头,// (可参考video标签学习 xgplayer视频播放器分段播放mp4 - https://blog.csdn.net/qq_16992475/article/details/130945997)String rangeHeader request.getHeader(Range);// 没有Range请求头, 则下载整个文件if (rangeHeader null) {response.setHeader(Content-Length, String.valueOf(fileSize));InputStream in new FileInputStream(file);OutputStream out response.getOutputStream();// 字节缓冲数组byte[] buffer new byte[BUFFER_SIZE];int bytesRead -1;// 读取多少, 写多少, 直到读取完毕为止while ((bytesRead in.read(buffer)) ! -1) {out.write(buffer, 0, bytesRead);}in.close();out.close();} else {// 分片下载// (可参考: 参考ResourceHttpRequestHandler#handleRequest中的做法)// 开始索引long start 0;// 结束索引long end fileSize - 1;// 获取Range请求头的范围, 格式为Range: bytes0-8055,// (其中可能没有结束位置, 若没有位置, 取文件大小-1)String[] range rangeHeader.split()[1].split(-);// 如果Range请求头中没有结束位置, 取文件大小-1if (range.length 1) {start Long.parseLong(range[0]);end fileSize - 1;} else {// 解析开始位置 和 结束位置start Long.parseLong(range[0]);end Long.parseLong(range[1]);}// 此次要写出的数据long contentLength end - start 1;// 返回头里存放每次读取的开始和结束字节response.setHeader(Content-Length, String.valueOf(contentLength));// 响应状态码206response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);// Content-Range响应头格式为Content-Range: bytes 0-8055/9000response.setHeader(Content-Range, bytes start - end / fileSize);InputStream in new FileInputStream(file);OutputStream out response.getOutputStream();// 跳到第start字节in.skip(start);// 字节缓冲数组byte[] buffer new byte[BUFFER_SIZE];// 读取的字节数量int bytesRead -1;// 写出的字节数量long bytesWritten 0;while ((bytesRead in.read(buffer)) ! -1) {// 如果 已写入的数据 当前已读到的数据 超过了 此次要写出的数据, 则只能写入请求范围内的数据if (bytesWritten bytesRead contentLength) {out.write(buffer, 0, (int) (contentLength - bytesWritten));break;} else {out.write(buffer, 0, bytesRead);bytesWritten bytesRead;}}in.close();out.close();}}}ChunkDownload.vue
先发1个head请求获取到文件的大小再发post请求获取每个分片其中为了简单理解就不引入async-await的使用了将获取的每个分片组合为单个文件
templatediv classgapel-button clickdownloadChunks分片下载demo.mp4/el-button/div
/templatescript
import axios from axiosexport default {name: ChunkDownload,components: {},methods: {downloadChunks() {const chunkdownloadUrl http://localhost:8085/chunkdownload// 分片下载大小 5MBconst chunkSize 1024 * 1024 * 5;// 文件总大小(需要请求后端获得)let fileSize 0;axios.head(chunkdownloadUrl).then(res {// 定义 存储所有的分片的数组let chunks [];// 获取文件总大小fileSize res.headers[content-length]// 计算分片数量const chunksNum Math.ceil(fileSize / chunkSize)// 定义下载文件分片的方法function downloadChunkFile(chunkIdx) {if (chunkIdx chunksNum) {alert(分片索引不可超过分片数量)return}let start chunkIdx * chunkSizelet end Math.min(start chunkSize - 1, fileSize - 1)const range bytes${start}-${end};axios({url: chunkdownloadUrl,method: post,headers: {Range: range},responseType: arraybuffer}).then(response {chunks.push(response.data)if(chunkIdx chunksNum - 1) {// 下载好了console.log(chunks, chunks);// 组合chunks到单个文件const blob new Blob(chunks);console.log(blob, blob);const link document.createElement(a);link.href window.URL.createObjectURL(blob);link.download demo.mp4;link.click();return} else {chunkIdxdownloadChunkFile(chunkIdx)}})}downloadChunkFile(0)})}}
}
/scriptstyle
.gap {padding: 10px;
}
/style测试
文章转载自: http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.mmynk.cn.gov.cn.mmynk.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.dschz.cn.gov.cn.dschz.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.txtzr.cn.gov.cn.txtzr.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.rrms.cn.gov.cn.rrms.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.rgsnk.cn.gov.cn.rgsnk.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn