温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用springboot如何实现下载单或多的zip文件

发布时间:2020-11-07 15:03:28 来源:亿速云 阅读:1077 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用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文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI