Java web文件下载断点续传

一、下载文件请求 

    @RequestMapping(value = "/file/download")@ResponseBodypublic Res download(HttpServletRequest request, HttpServletResponse response) {File file = new File(request.getParameter("fileName"));if (file.exists()) {String range = request.getHeader("Range");if (range != null && (range = range.trim()).length() > 0) {Pattern rangePattern = Pattern.compile("^bytes=([0-9]+)-([0-9]+)?$");Matcher matcher = rangePattern.matcher(range);if (matcher.find()) {Integer start = Integer.valueOf(matcher.group(1));Integer end = 0;String endStr = matcher.group(2);if (endStr != null && (endStr = endStr.trim()).length() > 0)end = Integer.valueOf(endStr);downLoadByBreakpoint(file, start, end, response);return null;}}downLoadAll(file, response);return null;}return Res.fail("文件不存在");}

 

二、断点下载

 public void downLoadByBreakpoint(File file, long start, long end, HttpServletResponse response){OutputStream stream = null;RandomAccessFile fif = null;try {if (end <= 0) {end = file.length() - 1;}stream = response.getOutputStream();response.reset();response.setStatus(206);response.setContentType("application/octet-stream");response.setHeader("Content-disposition", "attachment; filename=" + file.getName());response.setHeader("Content-Length", String.valueOf(end - start + 1));response.setHeader("Accept-Ranges", "bytes");response.setHeader("Content-Range", String.format("bytes %s-%s/%s", start, end, file.length()));fif = new RandomAccessFile(file, "r");fif.seek(start);long index = start;int d;byte[] buf = new byte[10240];while (index <= end && (d = fif.read(buf)) != -1) {if (index + d > end) {d = (int)(end - index + 1);}index += d;stream.write(buf, 0, d);}stream.flush();} catch (Exception e) {try {if (stream != null)stream.close();if (fif != null)fif.close();} catch (Exception e11) {}}}

 

三、普通全量下载

    public void downLoadAll(File file, HttpServletResponse response){OutputStream stream = null;BufferedInputStream fif = null;try {stream = response.getOutputStream();response.reset();response.setContentType("application/octet-stream");response.setHeader("Content-disposition", "attachment; filename=" + file.getName());response.setHeader("Content-Length", String.valueOf(file.length()));fif = new BufferedInputStream(new FileInputStream(file));int d;byte[] buf = new byte[10240];while ((d = fif.read(buf)) != -1) {stream.write(buf, 0, d);}stream.flush();} catch (Exception e) {try {if (stream != null)stream.close();if (fif != null)fif.close();} catch (Exception e11) {}}}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322598.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ajax面试技术回答模板

ajax是什么&#xff1f; 缩写、核心 1.ajax就是异步的 JS 和 XML 的缩写&#xff0c;目前我们一般用 JSON 代替 XML。 2.该技术最核心概念是 XMLHttpRequest 对象&#xff0c;该对象可发起 HTTP 请求&#xff0c;我们可以监听其 readystate 的变化获得响应。 怎么用&#xff…

微软人工智能和对话平台--知识商城体验

前言微软最新发布 知识商城了&#xff01;这是一个人工智能和对话平台应用的场景。他可以让开发者带着想法 出做天马行空的创造性工作&#xff01;你只需要稍微动动手&#xff0c;如&#xff1a;拖拽板块&#xff0c;就可以做到极致对答、代码自动生成&#xff01;想象一下&…

P1375-小猫【卡特兰数】

正题 题目链接:https://www.luogu.org/problemnew/show/P1375 题目大意 东西两两绑在一起&#xff0c;要求绳子不能交叉&#xff0c;求方案数。 解题思路 0表示压入第i只猫&#xff0c;1表示弹出栈顶的猫并且和第i只猫绑在一起&#xff0c;这样就能保证不会交叉。 也就是卡特…

Spring @Import注解配置类方法内部调用没有注入属性值的坑

一、场景复现 application.yaml spring:application:name: config-testprofiles:active: devconfig:config-01:name: zhansancode: 001config-02:name: lisicode: 002导入配置类 Configuration Import(ImportConfig.class) public class Config {BeanConfigurationPropertie…

使用Xamarin开发手机聊天程序 -- 基础篇(大量图文讲解 step by step,附源码下载)

如果是.NET开发人员&#xff0c;想学习手机应用开发&#xff08;Android和iOS&#xff09;&#xff0c;Xamarin 无疑是最好的选择&#xff0c;编写一次&#xff0c;即可发布到Android和iOS平台&#xff0c;真是利器中的利器啊&#xff01;而且&#xff0c;Xamarin已经被微软收购…

P3441-[POI2006]MET-Subway【图论,贪心】

正题 题目链接:https://www.luogu.org/problemnew/show/P3441 题目大意 求III条路径最多可以覆盖树上多少个点。 解题思路 我们先只考虑叶子节点&#xff0c;显然可以覆盖min{num叶,I∗2}min\{num_叶,I*2\}min{num叶​,I∗2}。 然后网上递推&#xff0c;发现依旧是min{numi,…

ssm创建一个查询接口

注解&#xff1a; controller Autowiredprivate UserService userService;service实体类 Service("userService")Autowiredprivate UserMapper userMapper;mapper Repositorycontroller 接收数据 > service 逻辑中转 > dao 数据库查询 > domain bean类映…

Spring Boot 数据库连接池入门

转载自 芋道 Spring Boot 数据库连接池入门 本文在提供完整代码示例&#xff0c;可见 https://github.com/YunaiV/SpringBoot-Labs 的 lab-19 目录。 原创不易&#xff0c;给点个 Star 嘿&#xff0c;一起冲鸭&#xff01; 1. 概述 在我们的项目中&#xff0c;数据库连接池基…

.net core 实现简单爬虫—抓取博客园的博文列表

一.介绍一个Http请求框架HttpCode.CoreHttpCode.Core 源自于HttpCode&#xff08;传送门&#xff09;&#xff0c;不同的是 HttpCode.Core是基于.net standard 2.0实现的&#xff0c;移除了HttpCode与windows相耦合的api&#xff0c;且修改了异步实现&#xff0c;其余特性完全与…

P3216-[HNOI2011]数学作业【矩阵乘法,数学】

正题 题目链接:https://www.luogu.org/problemnew/show/P3216 题目大意 求1∼n1\sim n1∼n连起来%m\% m%m之后的值。 解题思路 我们可以考虑矩乘&#xff0c;但是当xxx位数时每次乘上10x10^x10x&#xff0c;所以我们对于不同位分开处理就好了。 codecodecode #include<c…

spring boot使用注解的方式整合mybaits

使用注解整和mybatis&#xff0c;不需要任何的xml注释&#xff0c;只需要在 SpringBootApplication 加上一行mapper的扫描文件即可 MapperScan("com.k1998.mybatis.mapper")在application.properties配置 server.port8000 server.context-path/test#编码格式 serv…

SpringBoot2.1.9 Mybatis由于@Mapper注解多数据源配置不生效问题

一、场景复现 &#xff08;1&#xff09;项目 目录 配置文件 spring:application:name: multi-datasourceprofiles:active: dev1datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/base?…

使用Windows兼容包简化向.NET Core的迁移

从.NET迁移到.NET Core的一个主要原因&#xff0c;在于后者具备在Linux上运行的能力。但是对于大型企业应用&#xff0c;不可能实现一步迁移到位。由此&#xff0c;Microsoft推荐采用一种逐步迁移做法&#xff1a;第一步&#xff0c;迁移到ASP.NET Core&#xff08;依然使用.NE…

nssl1304-最大正方形【二分答案】

正题 题目大意 一个N∗NN*NN∗N的01矩阵&#xff0c;求一个面积最大的全为1的正方形 解题思路 先O(n2)O(n^2)O(n2)预处理hi,jh_{i,j}hi,j​表示在(i,j)(i,j)(i,j)这个位置向右有多少个连续的1。然后二分边长。 时间复杂度:O(n2logn):O(n^2\ log\ n):O(n2 log n) codecodecode…

springboot使用xml配置mybatis

前面用注解配置了mybatis&#xff0c;非常的简单&#xff0c;但是在写动态sql语句的时候会非常的麻烦&#xff0c;所以这边我们用xml来重新配置一下 在resource目录下新建 SqlMapConfig.xml 主配置文件 <?xml version"1.0" encoding"UTF-8" ?> &…

SpringBoot2.1.9 Mybatis多数据源配置

一、配置文件 目录 application.yaml spring:application:name: multi-datasourceprofiles:active: devdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/base?autoReconnecttrue&ze…

自动类型安全的REST .NET标准库refit

在SCOTT HANSELMAN 博客上看到一个好东西《Exploring refit, an automatic type-safe REST library for .NET Standard》&#xff0c;他推荐了一个.NET标准1.4 的自动类型安全的REST库refit。 refit 类似于Java的Retrofit&#xff0c;是一套RESTful架构的.NET客户端实现&#x…

nssl1305-最大值【dp,数学】

正题 题目大意 求有多少个长度为nnn且由1∼p1\sim p1∼p组成的序列满足在求最大值时交换了kkk次。 解题思路 考虑dpdpdp预处理。 用fi,j,kf_{i,j,k}fi,j,k​表示长度为iii&#xff0c;最大的数是jjj&#xff0c;交换了kkk次 显然有fi,j,kfi−1,p,k−1fi−1,j,k∗j(p<j)f…

spring boot常用注解的作用

Controller层注解 Controller和RestController的区别 RestController注解相当于ResponseBody &#xff0b; Controller合在一起的作用 如果只是使用RestController&#xff0c;无法返回jsp或者html页面 如果使用Controller&#xff0c;需要返回json数据&#xff0c;则必须加…

Visual Studio的语言服务器协议

语言服务器协议&#xff08;LSP&#xff09;是Visual Studio Code的一个重要组件。语言服务器实际上是单独运行的编译器或分析器&#xff0c;它负责处理各种任务&#xff0c;如编译器错误报告、文本悬浮、代码自动完成&#xff08;也就是IntelliSense&#xff09;等。语言服务器…