栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

单文件下载

//下载单个文件
public void downloadFile(HttpServletResponse response){
    String path = "D:testce1.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:testce1.txt";
    String test2= "D:testce2.txt";

    File tfile= new File(test1);
    File cfile= new File(test2);

    List files = new ArrayList<>();
    files.add(tfile);
    files.add(cfile);
    if (tfile.exists() && cfile.exists()) {

      String zipTmp = "D:testce1.zip";
      zipd(zipTmp,files,response);

     
    }
  }

public void zipd(String zipTmp,List 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文件压缩下载内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号