欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot單文件下載和多文件壓縮zip下載的實現(xiàn)

 更新時間:2020年11月06日 10:51:51   作者:二十同學  
這篇文章主要介紹了springboot單文件下載和多文件壓縮zip下載的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

單文件下載

//下載單個文件
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();
      // 創(chuàng)建文件輸出流
      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下載的實現(xiàn)的文章就介紹到這了,更多相關springboot文件壓縮下載內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java文件操作輸入輸出結構詳解

    java文件操作輸入輸出結構詳解

    這篇文章主要介紹了java文件操作輸入輸出詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • 深入淺出理解Java Lambda表達式之四大核心函數(shù)式的用法與范例

    深入淺出理解Java Lambda表達式之四大核心函數(shù)式的用法與范例

    Lambda 表達式,也可稱為閉包,它是推動 Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個方法的參數(shù)(函數(shù)作為參數(shù)傳遞進方法中)。使用 Lambda 表達式可以使代碼變的更加簡潔緊湊,今天小編帶你理解Lambda表達式之四大核心函數(shù)式的用法,感興趣的朋友快來看看吧
    2021-11-11
  • MyBatis Mapper.xml中的命名空間及命名方式

    MyBatis Mapper.xml中的命名空間及命名方式

    這篇文章主要介紹了MyBatis Mapper.xml中的命名空間及命名方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java 源碼分析Arrays.asList方法詳解

    java 源碼分析Arrays.asList方法詳解

    這篇文章主要介紹了java 源碼分析Arrays.asList方法詳解的相關資料,需要的朋友可以參考下
    2016-10-10
  • Java獲取resources下文件路徑的幾種方法及遇到的問題

    Java獲取resources下文件路徑的幾種方法及遇到的問題

    這篇文章主要給大家介紹了關于Java獲取resources下文件路徑的幾種方法及遇到的問題,在Java開發(fā)中經常需要讀取項目中resources目錄下的文件或獲取資源路徑,需要的朋友可以參考下
    2023-12-12
  • 自己動手實現(xiàn)mybatis動態(tài)sql的方法

    自己動手實現(xiàn)mybatis動態(tài)sql的方法

    下面小編就為大家分享一篇自己動手實現(xiàn)mybatis動態(tài)sql的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 淺談Spring Boot日志框架實踐

    淺談Spring Boot日志框架實踐

    這篇文章主要介紹了淺談Spring Boot日志框架實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • JDK線程池和Spring線程池的使用實例解析

    JDK線程池和Spring線程池的使用實例解析

    這篇文章主要介紹了JDK線程池和Spring線程池的使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • 詳解使用Spring的BeanPostProcessor優(yōu)雅的實現(xiàn)工廠模式

    詳解使用Spring的BeanPostProcessor優(yōu)雅的實現(xiàn)工廠模式

    這篇文章主要介紹了詳解使用Spring的BeanPostProcessor優(yōu)雅的實現(xiàn)工廠模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • 全面詳解Spring?Bean生命周期教程示例

    全面詳解Spring?Bean生命周期教程示例

    這篇文章主要為大家介紹了Spring?Bean生命周期的全面詳解教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04

最新評論