日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Java 壓縮圖片并打包成ZIP文件的示例

瀏覽:13日期:2022-08-20 09:42:57

JAVA 獲取網絡圖片或本地圖片壓縮后打成ZIP,但是獲取網絡流存在問題:每次獲取圖片流的大小不一樣(圖片不完整),以致無法構建圖片進行壓縮?

/* 釋以下代碼:即可獲取完整圖片流網絡不穩定情況且網絡流是順序讀取,所以獲得前部份流,不需要關閉連接,只需要將用完的流關閉即可 */ finally{ if(httpCon != null) httpCon.disconnect(); }

package com.sunshine.monitor.comm.util.http; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import sun.net.www.protocol.ftp.FtpURLConnection; /** * 獲取網絡圖片 * * @author OY */ public abstract class HttpHelpers {private static final String KEY = 'file.encoding';private static final String ENCODING = 'GBK';public static InputStream getInputStream(String url) throws Exception{ URLConnection con = null; HttpURLConnection httpCon = null; FtpURLConnection ftpCon= null; try { System.setProperty(KEY, ENCODING); URL _url = new URL(url); con = _url.openConnection(); con.setConnectTimeout(3000); con.setUseCaches(false);// 不緩存 con.setDefaultUseCaches(false); if (con instanceof HttpURLConnection) { httpCon = (HttpURLConnection) con; httpCon.setInstanceFollowRedirects(true); //httpCon.setRequestProperty('Accept-Charset', 'utf-8'); if (httpCon.getResponseCode() >= 300) { System.out.println('URL:' + url + ',HTTP Request is not success, Response code is ' + httpCon.getResponseCode()); } else { return httpCon.getInputStream(); } } else if(con instanceof FtpURLConnection){ ftpCon = (FtpURLConnection)con; return ftpCon.getInputStream(); } } catch (Exception e) { e.printStackTrace(); }finally{ if(httpCon != null) httpCon.disconnect(); } return null; } public static void main(String[] args) { // 1圖片本地存儲大小 OutputStream fout = null; InputStream input = null; try { fout = new FileOutputStream('F:' + File.separator + '1.jpg'); input = getInputStream('http://192.168.1.200/t.jpg'); byte[] buffer = new byte[1024]; int count = 0 ; while((count=input.read(buffer)) != -1){ fout.write(buffer, 0, count); } fout.flush(); } catch (Exception e) { e.printStackTrace(); } finally{ try { if(input != null) input.close(); if(fout != null) fout.close(); } catch (IOException e) { e.printStackTrace(); } } // 2是否可以構建圖片 try { input = getInputStream('http://192.168.1.200/t.jpg'); ImageInputStream iis = ImageIO.createImageInputStream(input); if(iis != null) { Iterator<ImageReader> it = ImageIO.getImageReaders(iis); if(!it.hasNext()){ System.out.println('流不完整或不是圖片!'); } else { System.out.println(it.next().getFormatName()); } } } catch (Exception e) { e.printStackTrace(); } } }

圖片壓縮采用thumbnailator,可以按大小、按比例、按質量壓縮并增加水印,API簡單

package com.sunshine.monitor.comm.util.compress; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions; /** * 圖片壓縮:按大小、按比例壓縮、按質量 * 增加水印 * @author OY * */ public abstract class CompressPictureTools {private static float QUALITY = 0.6f; /** * 按大小縮小 * * @param file * @param width * @param height * @return * @throws Exception */ public static byte[] compressOfSize(File file, int width, int height) throws Exception { byte[] bs = null; InputStream input = null; try { input = new FileInputStream(file); bs = compressOfSize(input, width, height); } catch (Exception e) { e.printStackTrace(); } finally { try { if (input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } return bs; }/** * 按大小縮小 * * @param input 原圖 * @param width 目標寬席 * @param height 目標高度 * @return * @throws Exception */ public static byte[] compressOfSize(InputStream input, int width, int height) throws Exception { ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); Thumbnails.of(input).size(width, height).toOutputStream(output); return output.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }/** * 按指定比例進行縮小和放大: percent=1不變 percent>1放大 percent<1縮小 * * @param input 原圖 * @param percent 壓縮比例 * @return * @throws Exception */ public static byte[] compressOfPercent(InputStream input, float percent) throws Exception { ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); Thumbnails.of(input).scale(percent).toOutputStream(output); return output.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }/** * 按指定比例進行縮小和放大: percent=1不變 percent>1放大 percent<1縮小 * * @param file 原圖 * @param percent 壓縮比例 */ public static byte[] compressOfPercent(File file, float percent) throws Exception { byte[] bs = null; InputStream input = null; try { input = new FileInputStream(file); bs = compressOfPercent(input, percent); } catch (Exception e) { e.printStackTrace(); } finally { try { if (input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } return bs; }/** * 按質量壓縮:圖片尺寸不變,壓縮圖片文件大小 * * @param file 原圖 * @param quality * =1為最高質量 * @return * @throws Exception */ 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; }/** * 按質量壓縮:圖片尺寸不變,壓縮圖片文件大小 * * @param input 原圖 * @param quality * =1為最高質量 * @return * @throws Exception */ 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; }/** * 圖片右下角添加水印 * * @param fromPic * 原圖 * @param markPic * 水印圖 * @param transparent * 透明度 * @return * @throws Exception */ public static byte[] waterMark(byte[] fromPic, InputStream markPic, float transparent) throws Exception { InputStream finput = null; ByteArrayOutputStream output = null; try { finput = new ByteArrayInputStream(fromPic); output = new ByteArrayOutputStream(); Thumbnails .of(finput) .scale(1f) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(markPic), transparent).toOutputStream(output); return output.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); if (finput != null) finput.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }/** * 圖片格式轉換 * * @param fromPic * 原圖 * @param picFormat * 格式 png,jpg... * @return * @throws Exception */ public static byte[] transferPicFormat(byte[] fromPic, String picFormat) throws Exception { ByteArrayInputStream finput = null; ByteArrayOutputStream output = null; try { finput = new ByteArrayInputStream(fromPic); output = new ByteArrayOutputStream(); Thumbnails.of(finput).outputFormat(picFormat) .toOutputStream(output); return output.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); if (finput != null) finput.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }

因JDK1.7以下,不可以設置編碼,以致中文亂碼問題,未采用java.util.ZipOutputStream,而是Apache ant下的ZipOutputStream

package com.sunshine.monitor.comm.util.compress; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import com.sunshine.monitor.comm.util.http.HttpHelpers; /** * 圖片壓縮成ZIP,支持并發多線程; * java.util.ZipOutputStream中文亂碼 * 方法一、JDK1.7可以設置編碼 * 方法二、換成Apache ant * @author OY * */ public class PicturePackZipTools { private static String DEFAULT_COMPRESS_ENCODE = 'GBK'; private static ZipOutputStream getZipStreamEncode(OutputStream output, String encode) { ZipOutputStream zipStream = new ZipOutputStream(output); if (encode == null || ''.equals(encode)) { zipStream.setEncoding(DEFAULT_COMPRESS_ENCODE); } else { zipStream.setEncoding(encode); } return zipStream; }/** * 訪問本地路徑下的所有文件 * * @param path * @return */ public static List<File> loadFiles(String path) { List<File> list = null; try { File fold = new File(path); if (fold.isDirectory()) { File[] files = fold.listFiles(); list = Arrays.asList(files); } } catch (Exception e) { e.printStackTrace(); } return list; }/** * 讀取本地系統路徑下的所有圖片打成ZIP * * @param path * @param output * @param compress */ public static void compressZip(String path, OutputStream output, String encode, boolean compress) { List<File> listfiles = null; ZipOutputStream zipStream = null; try { zipStream = getZipStreamEncode(output, encode); listfiles = loadFiles(path); for (File file : listfiles) { compressZip(file, zipStream, compress); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (zipStream != null) { zipStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }/** * 讀取網絡圖片打成打成ZIP * @param urls * key = 圖片名, value = 圖片URL * @param output * @param encode 編碼 * @param compress 是否壓縮 */ public static void compressZip(Map<String, String> urls, OutputStream output, String encode, boolean compress) { ZipOutputStream zipStream = null; try { zipStream = getZipStreamEncode(output, encode); Map<String, String> synUrls = Collections.synchronizedMap(urls); Set<Entry<String, String>> set = synUrls.entrySet(); Iterator<Entry<String, String>> it = set.iterator(); while (it.hasNext()) { Entry<String, String> entry = it.next(); compressZip(entry.getValue(), zipStream, entry.getKey(), compress); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (zipStream != null) { zipStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }/** * 壓縮單個文件為ZIP * @param file * @param output * @param encode * @param compress */ public static void compressZip(File file, OutputStream output, String encode, boolean compress) throws Exception{ FileInputStream input = null; try { input = new FileInputStream(file); compressZip(input,file.getName(),output,encode,compress); } catch (Exception e) { e.printStackTrace(); } finally { try { if (input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } }}/** * 壓縮單個文件為ZIP * @param input * @param fileName * @param output * @param encode * @param compress */ public static void compressZip(InputStream input, String fileName, OutputStream output, String encode, boolean compress) throws Exception { ZipOutputStream zipStream = null; try { zipStream = getZipStreamEncode(output, encode); zip(input, zipStream, fileName, compress); } 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) throws Exception{ FileInputStream input = null; try { input = new FileInputStream(file); zip(input, zipStream, file.getName(), compress); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } }/** * 網絡圖片 * * @param url * @param zipStream * @param compress */ private static void compressZip(String url, ZipOutputStream zipStream, String fileName, boolean compress) throws Exception{ InputStream input = null; try { input = HttpHelpers.getInputStream(url); zip(input, zipStream, fileName, compress); } catch (Exception e) { e.printStackTrace(); } finally{ try { if(input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } }/** * @param input * @param zipStream * @param zipEntryName * @param compress */ 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 = CompressPictureTools.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(); } } } }

以上就是Java 壓縮圖片并打成ZIP文件的示例的詳細內容,更多關于Java 壓縮圖片打包成zip的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产美女撒尿一区二区| 久久久影院免费| 欧美成人基地| 欧美日韩精品免费观看视欧美高清免费大片 | 日韩高清一级| 午夜久久av| 日本h片久久| 国产日产高清欧美一区二区三区| 日本视频一区二区| 久久成人高清| 亚洲精品永久免费视频| 天堂av在线| 91九色精品| 亚洲欧美久久久| 91成人在线精品视频| 欧美aa在线视频| 日韩在线不卡| 亚洲综合不卡| 国产精品成人国产| 日本免费久久| 日精品一区二区三区| 美女在线视频一区| 欧美影院三区| 欧美日韩99| 欧美不卡在线| 精品亚洲成人| 午夜一级久久| 色婷婷综合网| 日本亚洲欧美天堂免费| 激情久久99| 亚洲青青久久| 国产一区调教| 亚洲欧美日韩高清在线| 日韩不卡一区二区| 国产一区亚洲| 国产精品伊人| 丝瓜av网站精品一区二区 | 亚洲一区二区三区免费在线观看| 日韩精品一区二区三区中文在线 | 一区二区三区国产盗摄| 国产伊人久久| 清纯唯美亚洲综合一区| 自拍日韩欧美| 一区二区精品伦理...| 亚洲精品字幕| 99国产精品久久久久久久成人热 | 夜夜精品视频| 亚洲爱爱视频| 国产a亚洲精品| 国产日韩1区| 少妇精品久久久一区二区| 欧美日韩一二| 日韩国产欧美一区二区| 国产精品九九| 欧美成人精品一级| 国产美女亚洲精品7777| 蜜芽一区二区三区| 一级欧洲+日本+国产| 久久精品播放| 久久中文字幕av一区二区不卡| 九九久久国产| 免费在线亚洲欧美| 日韩国产欧美在线播放| 欧美大黑bbbbbbbbb在线| 国产传媒在线| 成人av三级| 日韩精品91| 婷婷精品视频| 美女毛片一区二区三区四区 | 99精品电影| 激情婷婷久久| 国产白浆在线免费观看| 久久精品色播| 日韩伦理在线一区| se01亚洲视频| 精品中文一区| 丝袜脚交一区二区| 日韩国产在线不卡视频| 日本午夜精品一区二区三区电影| 首页欧美精品中文字幕| 三级在线观看一区二区 | 日韩高清一区| 国产欧美日本| 久久av免费看| 一区二区精品伦理...| 特黄特色欧美大片| 91精品观看| 日本精品久久| 一本大道色婷婷在线| 亚洲欧美日韩精品一区二区| 亚洲精品影院在线观看| 国产免费播放一区二区| 91看片一区| 亚洲日产av中文字幕| 久久的色偷偷| 日韩午夜av在线| 国产欧美大片| 91久久在线| 青草av.久久免费一区| 日韩精品dvd| 日韩亚洲精品在线观看| 高潮久久久久久久久久久久久久| 在线亚洲免费| 国产精品精品国产一区二区| 免费久久99精品国产自在现线| 国产乱码精品一区二区三区四区 | 综合国产在线| 桃色av一区二区| 97se亚洲| 国产综合精品| 麻豆精品久久久| 国产一区二区精品| 久久不卡日韩美女| 夜夜嗨网站十八久久| 韩日一区二区| 日本欧美一区| 亚洲综合精品四区| 99精品综合| 国产中文字幕一区二区三区| 国产亚洲毛片| 91精品啪在线观看国产18 | 国产一区二区三区不卡av| 亚洲a级精品| 久久福利一区| 欧美在线资源| 国产精品99一区二区三区| 日韩在线播放一区二区| 亚洲一级特黄| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 国产精品婷婷| 欧美 日韩 国产一区二区在线视频| 国产精品自拍区| 国产精品美女在线观看直播| 日韩av资源网| 国产视频一区二| 免费亚洲婷婷| 国产在线不卡一区二区三区 | 高清av一区二区三区| 国产中文在线播放| 精品久久97| 天堂а√在线最新版中文在线| caoporn视频在线| 日本免费一区二区三区四区| 日韩精品欧美| 欧美日韩精品一本二本三本| 欧美日韩国产亚洲一区| 视频一区视频二区中文字幕| 美女久久网站| 久久国产免费看| 伊人久久大香线蕉av超碰演员| 在线 亚洲欧美在线综合一区| 另类激情亚洲| 国产日韩欧美一区二区三区 | 日韩另类视频| 国产精品日本| 久久av资源| 韩国三级一区| 亚洲欧洲日韩| 国产精品s色| 欧美中文一区二区| 91成人精品在线| 国产aⅴ精品一区二区三区久久| 午夜日韩在线| 91欧美日韩| 亚洲精品人人| 国产自产自拍视频在线观看| 日韩亚洲精品在线| 久久精品97| 欧美 日韩 国产一区二区在线视频| 日韩三级视频| 欧美 日韩 国产一区二区在线视频| 日韩欧美精品一区二区综合视频| 久久青青视频| 久久精品xxxxx| 免费一区二区视频| 99久久婷婷这里只有精品| 国产亚洲精品精品国产亚洲综合| 一区三区视频| 香蕉精品久久| 久久国产电影| 日韩成人亚洲| 精品三级av在线导航| 日韩精品视频网| 午夜国产精品视频| 日韩精品免费一区二区三区| 国产精品magnet| 亚洲欧美日韩一区在线观看| 中文字幕成在线观看| 日本精品一区二区三区在线观看视频| 中文字幕人成乱码在线观看| 亚洲精品四区| 日韩精品免费一区二区在线观看 | 中文字幕一区二区精品区| 国产免费av一区二区三区| 久久国产免费看| 另类av一区二区| 香蕉国产精品| 久久蜜桃av| 日本精品不卡| 波多视频一区|