亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java多文件以ZIP壓縮包導出的實現方法

 更新時間:2018年07月14日 13:14:39   作者:白楓J  
這篇文章主要為大家詳細介紹了Java多文件以ZIP壓縮包導出的實現方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java多文件以ZIP壓縮包導出的具體代碼,供大家參考,具體內容如下

1、使用java實現吧服務器的圖片打包成一個zip格式的壓縮包導出,多個文件打包導出。
2、代碼如下:

**ImageByteUtil.java**

public class ImageByteUtil{
  private static float QUALITY = 0.6f;
  public static void compressZip(List<File> listfiles, OutputStream output,String encode, boolean compress,String alias){
  ZipOutputStream zipStream = null;
  try {
      zipStream = new ZipOutputStream(output);
      for (File file : listfiles){
        compressZip(file, zipStream, compress,alias+"_"+(listfiles.indexOf(file)+1));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      try { 
        if (zipStream != null) { 
          zipStream.close(); 
        } 
      } catch (IOException e) { 
        e.printStackTrace(); 
      }
    }
  }

private static void compressZip(File file, ZipOutputStream zipStream, 
      boolean compress,String alias) throws Exception{
    FileInputStream input = null;
    try {
      input = new FileInputStream(file); 
      //zip(input, zipStream, file.getName(), compress); 
      zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1), compress);
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      try { 
        if(input != null) 
          input.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      }
    }
  }

private static void zip(InputStream input, ZipOutputStream zipStream, 
      String zipEntryName, boolean compress) throws Exception{
      byte[] bytes = null; 
    BufferedInputStream bufferStream = null; 
    try { 
      if(input == null) 
        throw new Exception("獲取壓縮的數據項失敗! 數據項名為:" + zipEntryName); 
      // 壓縮條目不是具體獨立的文件,而是壓縮包文件列表中的列表項,稱為條目,就像索引一樣 
      ZipEntry zipEntry = new ZipEntry("圖片/"+zipEntryName);
      // 定位到該壓縮條目位置,開始寫入文件到壓縮包中 
      zipStream.putNextEntry(zipEntry);
      if (compress) { 
        bytes = ImageByteUtil.compressOfQuality(input, 0); 
        zipStream.write(bytes, 0, bytes.length); 
      } else {
        bytes = new byte[1024 * 5];// 讀寫緩沖區(qū) 
        bufferStream = new BufferedInputStream(input);// 輸入緩沖流 
        int read = 0; 
        while ((read = bufferStream.read(bytes)) != -1) {
          zipStream.write(bytes, 0, read); 
        } 
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (null != bufferStream) 
          bufferStream.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    }
  }

  public static byte[] compressOfQuality(File file, float quality) throws Exception{
    byte[] bs = null; 
    InputStream input = null; 
    try { 
      input = new FileInputStream(file); 
      bs = compressOfQuality(input,quality);
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (input != null) 
          input.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return bs;
  }

  public static byte[] compressOfQuality(InputStream input, float quality) 
      throws Exception {
      ByteArrayOutputStream output = null; 
    try { 
      output = new ByteArrayOutputStream(); 
      if(quality == 0){ 
        Thumbnails.of(input).scale(1f).outputQuality(QUALITY) 
        .toOutputStream(output); 
      } else { 
        Thumbnails.of(input).scale(1f).outputQuality(quality).toOutputStream(output); 
      } 
      return output.toByteArray(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (output != null) 
          output.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return null;
  }
}

**Main.java**

public static void main(String[] args){
  //要導出的文件集合,添加自己需要導出的文件
  List<File> ListFiles = new ArrayList<>();
  //調用工具類,參數說明(需要導出的文件集,ByteArrayOutputStream對象,編碼,是否壓縮【true,false】,文件名稱前綴)
  ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ");
  //指定導出格式
  ReturnContext.addParam("exportFileName","extFile.zip");
  ReturnContext.addParam("mimeType", "zip");
  return in;
}

3、此功能是根據在開發(fā)過程中項目需要實現的,測試可正常使用,可更改定制。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • maven+springboot打成jar包的方法

    maven+springboot打成jar包的方法

    這篇文章主要介紹了maven+springboot打成jar包的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • java代碼獲取數據庫表里數據的總數操作

    java代碼獲取數據庫表里數據的總數操作

    這篇文章主要介紹了java代碼獲取數據庫表里數據的總數操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 詳解Java?POI?excel自定義設置單元格格式

    詳解Java?POI?excel自定義設置單元格格式

    這篇文章主要介紹了Java?POI?excel設置單元格格式,自定義設置,設置單元格格式:來源_formats,更多數據類型從formats里面發(fā)現,需要的朋友可以參考下
    2024-01-01
  • Spring注入Bean的一些方式總結

    Spring注入Bean的一些方式總結

    這篇文章主要給大家總結介紹了關于Spring注入Bean的一些方式,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • java樹形菜單對象生成

    java樹形菜單對象生成

    這篇文章主要為大家詳細介紹了java樹形菜單對象生成,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題

    解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題

    這篇文章主要介紹了解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot的@Value注解如何設置默認值

    SpringBoot的@Value注解如何設置默認值

    這篇文章主要介紹了SpringBoot的@Value注解如何設置默認值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java中ArrayList在foreach里remove的問題詳析

    Java中ArrayList在foreach里remove的問題詳析

    這篇文章主要給大家介紹了關于Java中ArrayList在foreach里remove問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-09-09
  • Idea創(chuàng)建多模塊maven聚合項目的實現

    Idea創(chuàng)建多模塊maven聚合項目的實現

    這篇文章主要介紹了Idea創(chuàng)建多模塊maven聚合項目的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • SpringMVC通過注解獲得參數的實例

    SpringMVC通過注解獲得參數的實例

    下面小編就為大家?guī)硪黄猄pringMVC通過注解獲得參數的實例。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論