真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法-創(chuàng)新互聯(lián)

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

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到蒙城網(wǎng)站設(shè)計(jì)與蒙城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋蒙城地區(qū)。

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 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;
  }
}

網(wǎng)頁(yè)標(biāo)題:Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法-創(chuàng)新互聯(lián)
URL標(biāo)題:http://weahome.cn/article/dppcgg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部