99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

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

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

創新互聯-專業網站定制、快速模板網站建設、高性價比達日網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式達日網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋達日地區。費用合理售后完善,10多年實體公司更值得信賴。

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];// 讀寫緩沖區 
        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、此功能是根據在開發過程中項目需要實現的,測試可正常使用,可更改定制。

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

網頁名稱:Java多文件以ZIP壓縮包導出的實現方法
分享路徑:http://www.yijiale78.com/article26/pjdscg.html

成都網站建設公司_創新互聯,為您提供網站策劃品牌網站建設移動網站建設網站收錄手機網站建設動態網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司