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;
}
}
}
根据url读取图片,直接压缩写到zip流【含判断url地址是否存在】
最新推荐文章于 2023-12-17 22:57:09 发布