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

Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法

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

本文實(shí)例為大家分享了Java多文件以ZIP壓縮包導(dǎo)出的具體代碼,供大家參考,具體內(nèi)容如下

1、使用java實(shí)現(xiàn)吧服務(wù)器的圖片打包成一個(gè)zip格式的壓縮包導(dǎo)出,多個(gè)文件打包導(dǎo)出。
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("獲取壓縮的數(shù)據(jù)項(xiàng)失敗! 數(shù)據(jù)項(xiàng)名為:" + zipEntryName); 
      // 壓縮條目不是具體獨(dú)立的文件,而是壓縮包文件列表中的列表項(xiàng),稱為條目,就像索引一樣 
      ZipEntry zipEntry = new ZipEntry("圖片/"+zipEntryName);
      // 定位到該壓縮條目位置,開(kāi)始寫(xiě)入文件到壓縮包中 
      zipStream.putNextEntry(zipEntry);
      if (compress) { 
        bytes = ImageByteUtil.compressOfQuality(input, 0); 
        zipStream.write(bytes, 0, bytes.length); 
      } else {
        bytes = new byte[1024 * 5];// 讀寫(xiě)緩沖區(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){
  //要導(dǎo)出的文件集合,添加自己需要導(dǎo)出的文件
  List<File> ListFiles = new ArrayList<>();
  //調(diào)用工具類,參數(shù)說(shuō)明(需要導(dǎo)出的文件集,ByteArrayOutputStream對(duì)象,編碼,是否壓縮【true,false】,文件名稱前綴)
  ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ");
  //指定導(dǎo)出格式
  ReturnContext.addParam("exportFileName","extFile.zip");
  ReturnContext.addParam("mimeType", "zip");
  return in;
}

3、此功能是根據(jù)在開(kāi)發(fā)過(guò)程中項(xiàng)目需要實(shí)現(xiàn)的,測(cè)試可正常使用,可更改定制。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于synchronized、volatile、ReentrantLock的區(qū)別與對(duì)比

    關(guān)于synchronized、volatile、ReentrantLock的區(qū)別與對(duì)比

    這篇文章主要介紹了關(guān)于synchronized、volatile、ReentrantLock的區(qū)別與對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • spring BeanProcessor接口詳解

    spring BeanProcessor接口詳解

    這篇文章主要介紹了spring BeanProcessor接口的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用spring,感興趣的朋友可以了解下
    2021-03-03
  • Java設(shè)計(jì)模式之單例模式實(shí)例分析

    Java設(shè)計(jì)模式之單例模式實(shí)例分析

    這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式,以實(shí)例形式較為詳細(xì)的分析了單例模式的概念、定義及簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-11-11
  • AntDesign多環(huán)境配置啟動(dòng)過(guò)程詳解

    AntDesign多環(huán)境配置啟動(dòng)過(guò)程詳解

    這篇文章主要為大家介紹了AntDesign多環(huán)境配置啟動(dòng)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • mybatis中使用not?in與?in的寫(xiě)法說(shuō)明

    mybatis中使用not?in與?in的寫(xiě)法說(shuō)明

    這篇文章主要介紹了mybatis中使用not?in與?in的寫(xiě)法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能

    Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能

    這篇文章介紹了Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • 詳解Java內(nèi)存管理中的JVM垃圾回收

    詳解Java內(nèi)存管理中的JVM垃圾回收

    這篇文章給大家分享了關(guān)于Java內(nèi)存管理中的JVM垃圾回收的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2018-08-08
  • 關(guān)于線程池創(chuàng)建、執(zhí)行、銷毀的原理及分析

    關(guān)于線程池創(chuàng)建、執(zhí)行、銷毀的原理及分析

    這篇文章主要介紹了關(guān)于線程池創(chuàng)建、執(zhí)行、銷毀的原理及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Java實(shí)現(xiàn)直接插入排序和折半插入排序算法示例

    Java實(shí)現(xiàn)直接插入排序和折半插入排序算法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)直接插入排序和折半插入排序算法示例,文中對(duì)算法的思想和時(shí)間復(fù)雜度都有簡(jiǎn)單的講解,需要的朋友可以參考下
    2016-04-04
  • flink進(jìn)階富函數(shù)生命周期介紹

    flink進(jìn)階富函數(shù)生命周期介紹

    這篇文章主要為大家介紹了flink進(jìn)階富函數(shù)生命周期的舉例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評(píng)論