springboot单文件下载和多文件压缩zip下载的实现

 更新时间:2020年11月06日 10:51:51   作者:二十同学  
这篇文章主要介绍了springboot单文件下载和多文件压缩zip下载的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

单文件下载

//下载单个文件
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();
        // 清空response
        response.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下载的实现的文章就介绍到这了,更多相关springboot文件压缩下载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java类的加载时机

    Java类的加载时机

    这篇文章介绍了Java类的加载时机,文中通过示例代码介绍的非常详细。对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • mybatisplus解决驼峰命名映射问题详解

    mybatisplus解决驼峰命名映射问题详解

    这篇文章主要介绍了mybatisplus解决驼峰命名映射问题详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • java中的export方法实现导出excel文件

    java中的export方法实现导出excel文件

    这篇文章主要介绍了java中的export方法实现导出excel文件,文章围绕java导出excel文件的相关资料展开详细内容,需要的小伙伴可以参考一下
    2022-03-03
  • java 数据结构并查集详解

    java 数据结构并查集详解

    并查集是一种用来管理元素分组情况的数据结构。并查集可以高效地进行如下操作。本文将通过Java实现并查集,感兴趣的小伙伴可以了解一下
    2022-03-03
  • SpringBoot使用Jackson配置全局时间日期格式

    SpringBoot使用Jackson配置全局时间日期格式

    本文主要介绍了SpringBoot使用Jackson配置全局时间日期格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Redisson之分布式锁原理全面分析

    Redisson之分布式锁原理全面分析

    这篇文章主要介绍了Redisson分布式锁原理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • Spring Boot2开发之Spring Boot整合Shiro两种详细方法

    Spring Boot2开发之Spring Boot整合Shiro两种详细方法

    这篇文章主要介绍了Spring Boot2开发之Spring Boot整合Shiro详细方法,需要的朋友可以参考下
    2020-03-03
  • mac下修改idea的jvm运行参数解决idea卡顿的情况

    mac下修改idea的jvm运行参数解决idea卡顿的情况

    这篇文章主要介绍了mac下修改idea的jvm运行参数解决idea卡顿的情况,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Netty分布式客户端处理接入事件handle源码解析

    Netty分布式客户端处理接入事件handle源码解析

    这篇文章主要为大家介绍了Netty源码分析客户端流程处理接入事件handle创建,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03
  • Spring Boot 中的 Native SQL基本概念及使用方法

    Spring Boot 中的 Native SQL基本概念及使用方法

    在本文中,我们介绍了 Spring Boot 中的 Native SQL,以及如何使用 JdbcTemplate 和 NamedParameterJdbcTemplate 来执行自定义的 SQL 查询或更新语句,需要的朋友跟随小编一起看看吧
    2023-07-07

最新评论