微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路

单文件下载

//下载单个文件
public void downloadFile(HttpServletResponse response){String path = "D:\test\ce\1.txt"File file = new File(path);if(file.exists()){String fileName = file.getName();response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);download(response,file);}}public void download(HttpServletResponse response,File file){FileInputStream fis = null;BufferedInputStream bis = null;OutputStream os = null;try {os = response.getOutputStream();fis = new FileInputStream(file);bis = new BufferedInputStream(fis);byte[] buffer = new byte[bis.available()];int i = bis.read(buffer);while(i != -1){os.write(buffer, 0, i);i = bis.read(buffer);}} catch (Exception e) {e.printStackTrace();}try {bis.close();fis.close();os.close();} catch (IOException e) {e.printStackTrace();}}

多文件压缩下载

//多个文件,压缩成zip后下载
public void downloadMoreFile(HttpServletResponse response) {String test1= "D:\test\ce\1.txt";String test2= "D:\test\ce\2.txt";File tfile= new File(test1);File cfile= new File(test2);List<File> files = new ArrayList<>();files.add(tfile);files.add(cfile);if (tfile.exists() && cfile.exists()) {String zipTmp = "D:\test\ce\1.zip";zipd(zipTmp,files,response);}}public void zipd(String zipTmp,List<File> files,HttpServletResponse response){File zipTmpFile = new File(zipTmp);try {if (zipTmpFile.exists()) {zipTmpFile.delete();}zipTmpFile.createNewFile();response.reset();// 创建文件输出流FileOutputStream fous = new FileOutputStream(zipTmpFile);ZipOutputStream zipOut = new ZipOutputStream(fous);zipFile(files, zipOut);zipOut.close();fous.close();downloadZip(zipTmpFile, response);} catch (IOException e) {e.printStackTrace();}}//files打成压缩包public void zipFile(List files, ZipOutputStream outputStream) {int size = files.size();for (int i = 0; i < size; i++) {File file = (File) files.get(i);zipFile(file, outputStream);}}public void zipFile(File inputFile, ZipOutputStream ouputStream) {try {if (inputFile.exists()) {if (inputFile.isFile()) {FileInputStream IN = new FileInputStream(inputFile);BufferedInputStream bins = new BufferedInputStream(IN, 512);ZipEntry entry = new ZipEntry(inputFile.getName());ouputStream.putNextEntry(entry);int nNumber;byte[] buffer = new byte[512];while ((nNumber = bins.read(buffer)) != -1) {ouputStream.write(buffer, 0, nNumber);}bins.close();IN.close();} else {try {File[] files = inputFile.listFiles();for (int i = 0; i < files.length; i++) {zipFile(files[i], ouputStream);}} catch (Exception e) {e.printStackTrace();}}}} catch (Exception e) {e.printStackTrace();}}public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {if (file.exists() == false) {System.out.println("待压缩的文件目录:" + file + "不存在.");} else {try {// 以流的形式下载文件。InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");// 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理response.setHeader("Content-Disposition","attachment;filename=" + new String(file.getName().getBytes("GB2312"), "ISO8859-1"));toClient.write(buffer);toClient.flush();toClient.close();} catch (Exception ex) {ex.printStackTrace();} finally {try {File f = new File(file.getPath());f.delete();} catch (Exception e) {e.printStackTrace();}}}return response;}

springboot单文件下载和多文件压缩zip下载相关推荐

  1. java文件下载功能代码(单文件下载、多文件批量打包下载)——普遍适用

    一.前言   程序员在做web等项目的时候,往往都需要添加文件上传.下载.删除的功能,有时是单文件,有时多文件批量 操作,而这些功能的代码程序员可以自己收藏起来当成工具使用,这样,程序员在进行程序设计 ...

  2. javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)

      程序员在做web等项目的时候,往往都需要添加文件上传.下载.删除的功能,有时是单文件,有时多文件批量 操作,而这些功能的代码程序员可以自己收藏起来当成工具使用,这样,程序员在进行程序设计的时候就会 ...

  3. java 文件下载代码_java文件下载代码实例(单文件下载和多文件打包下载)

    这篇文章主要介绍了java文件下载代码实例(单文件下载和多文件打包下载),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 最近项目有需要写文件下载相关 ...

  4. easypoi导出多个Excel压缩zip下载

    easypoi导出多个Excel压缩zip下载 maven <!-- 导入和导出--><dependency><groupId>cn.afterturn</g ...

  5. Java将一个文件夹下多个文件压缩并下载(工作案例)

    Java将一个文件下多个文件压缩并下载,文件夹目录如下: 每个文件下都有文件,要求实现将文件夹"A2023001_检查"压缩成"A2023001.zip",如下 ...

  6. 继上一篇博客--javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)

    通过动态分配地址来提升javaweb文件下载接口的其兼容性和可扩展性: (上篇博文地址:https://blog.csdn.net/weixin_37766296/article/details/80 ...

  7. JAVA-阿里云OSS文件下载并将文件压缩为ZIP格式保存

    1. pom引入 <!--oss-->     <dependency>         <groupId>com.aliyun.oss</groupId&g ...

  8. java文件下载接口_javaweb通过接口来实现多个文件压缩和下载(包括单文件下载,多文件批量下载)...

    @RequestMapping("/uploads")public void recursion(String root, Vector vecFile,HttpServletRe ...

  9. linux把文件复制到压缩包里,Linux学习笔记(二十)文件压缩 zip压缩、tar打包、打包、解包...

    一.zip压缩 首先安装zip与unzip yum install -y zip/unzip zip 1.txt.zip 1.txt 压缩文件1.txt,压缩文件名称为1.txt.zip zip -r ...

最新文章

  1. Java序列化(Serialization)的理解
  2. 牛客网 牛客练习赛13 C.幸运数字Ⅲ-思维
  3. 成功解决Exception “unhandled ImportError“cannot import name ‘imread‘ from ‘scipy.misc‘
  4. 正则表达式中的字符类
  5. 通向架构师的道路(第十一天)之Axis2 Web Service(二)
  6. linux sudo 免密码
  7. Java类的继承关键字_Java的第八天(类的继承、super关键字的使用、方法的重写)...
  8. C#删除字符串倒数第几个字符后的所有字符串
  9. epoll在ET和LT模式下读写
  10. 前锋php人工智能_人工智能除了学习php,还应该学什么?
  11. MFC控件使用总结——CListCtrl
  12. 汇编语言二进制转十进制_汇编语言笔记(一)——基础知识
  13. servlet3.0理解
  14. anaconda安装-清华镜像库
  15. 数据签名 RSA算法
  16. MySQL重做日志(redo log)总结
  17. 【opencv】 报错:C2065 “CV_COVAR_ROWS”、“CV_COVAR_NORMAL”、“CV_COVAR_SCALE”: 未声明的标识符、
  18. SAP UD取消处理合订本
  19. 成功解决windows系统开机时,系统提示此windows副本不是正版
  20. python构造方法的方法名_构造方法是类的一个特殊方法,Python中它的名称为()。

热门文章

  1. Pygame 官方文档 - Tutorials - 逐行的黑猩猩教程(Line By Line Chimp)
  2. 微信小程序 富文本 换行问题 文本溢出使用省略号
  3. webuploader 实现图片批量上传
  4. 谷歌大脑团队任意图像风格化迁移论文详解,模型还可以跑在您本地浏览器里...
  5. 用java语言如何编写圆面积_用java语言编写一个圆面积的求法
  6. java8 朗姆表达式,Python基础
  7. oracle裁员原因_甲骨文中国裁员 部分员工不满补偿方案
  8. [SCOI2012]喵星球上的点名(后缀数组+莫队+ST表)
  9. it转正述职报告_IT试用期转正工作总结
  10. STM32+ULN2003驱动步进电机