欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-28 17:42:32
单文件下载//下载单个文件public void downloadfile(httpservletresponse response){ string path = "d:\test\ce\1....

单文件下载

//下载单个文件
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文件压缩下载内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!