根據url讀取圖片,直接壓縮寫到zip流【含判斷url地址是否存在】

package cn.w.market.app.utils;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import cn.w.market.modules.app.entity.AppWallpaperImage;

import com.thinkgem.jeesite.common.utils.StringUtils;

public class ImgCompressUtil {
	static final int BUFFER = 8192;
	/**
	 * 
	 * @Description:壓縮一組appWallpaperImageList
	 * @author:鄭安邦
	 * @time:2013年9月13日  下午4:55:53
	 * @param appWallpaperImageList
	 * @param zipFileRealFullPathName :壓縮包的實際地址"d://xxx//xx.zip"
	 */
	public static void compressURLImgs2Zip(List<AppWallpaperImage> appWallpaperImageList,String zipFileRealFullPathName){
		ZipOutputStream out = null;
		try {
			File zipFile = new File(zipFileRealFullPathName);
			if(!zipFile.exists()){
				zipFile.getParentFile().mkdir();
				zipFile.createNewFile();
			}
			FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
			CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,new CRC32());
			out = new ZipOutputStream(cos);
			for (AppWallpaperImage appWallpaperImage : appWallpaperImageList) {
				if(exists(appWallpaperImage.getImagePath())){
					downUrlImg2zip(appWallpaperImage.getImagePath(), out);
				}
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(out!=null){
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public  static void downUrlImg2zip(String urlAdd, ZipOutputStream out) {
		BufferedInputStream bufferedInputStream = null;
		try {
			URL url = new URL(urlAdd);
			int sufferPos = StringUtils.lastIndexOf(urlAdd, ".");
			String suffer = StringUtils.substring(urlAdd, sufferPos);
			String imgName = StringUtils.substring(urlAdd, StringUtils.lastIndexOf(urlAdd, "/")+1,sufferPos);
			String imgFullName = StringUtils.join(imgName,suffer);
			
			DataInputStream dataInputStream = new DataInputStream(url.openStream());
			bufferedInputStream = new BufferedInputStream(dataInputStream);
			out.putNextEntry(new ZipEntry(imgFullName));
			int byteRead = 0;  
			byte data[] = new byte[BUFFER];
			while((byteRead = bufferedInputStream.read(data,0,BUFFER)) != -1){  
				out.write(data, 0, byteRead);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				bufferedInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
		}
	}
	
	
	 static boolean exists(String URLName) {

	       try {
	           //設置此類是否應該自動執行 HTTP 重定向(響應代碼爲 3xx 的請求)。
	           HttpURLConnection.setFollowRedirects(false);

	           //到 URL 所引用的遠程對象的連接

	           HttpURLConnection con = (HttpURLConnection) new URL(URLName)

	                  .openConnection();

	           /* 設置 URL 請求的方法, GET POST HEAD OPTIONS PUT DELETE TRACE 以上方法之一是合法的,具體取決於協議的限制。*/

	           con.setRequestMethod("HEAD");

	           //從 HTTP 響應消息獲取狀態碼

	           return (con.getResponseCode() == HttpURLConnection.HTTP_OK);

	       } catch (Exception e) {

	           e.printStackTrace();

	           return false;

	        }

	    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章