根据url读取图片,直接压缩写到zip流【含判断url地址是否存在】

4 篇文章 0 订阅
3 篇文章 0 订阅
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;

	        }

	    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值