java url压缩_java中,根据url下载文件并压缩文件,解压出错

import com.jd.eptid.waremanager.common.tools.DateUtils;

import com.jd.eptid.waremanager.common.tools.DownloadUtil;

import java.io.IOException;

import java.util.Date;

public class MyTest {

public static void main(String args[]) throws IOException, InterruptedException {

final String targetFolderPath = "/Users/cdshilejian/Desktop/img/";

final String newZipFilePath = "/Users/cdshilejian/Desktop/new_" + DateUtils.format(new Date()) + ".zip";

final String[] urls = {

"http://e.hiphotos.baidu.com/image/pic/item/2f738bd4b31c8701ad467c1a2b7f9e2f0608ff5e.jpg",

"http://e.hiphotos.baidu.com/image/pic/item/2f738bd4b31c8701ad467c1a2b7f9e2f0608ff5e.jpg",

"http://e.hiphotos.baidu.com/image/pic/item/2f738bd4b31c8701ad467c1a2b7f9e2f0608ff5e.jpg",

"http://e.hiphotos.baidu.com/image/pic/item/2f738bd4b31c8701ad467c1a2b7f9e2f0608ff5e.jpg",

"http://e.hiphotos.baidu.com/image/pic/item/2f738bd4b31c8701ad467c1a2b7f9e2f0608ff5e.jpg",

"http://e.hiphotos.baidu.com/image/pic/item/2f738bd4b31c8701ad467c1a2b7f9e2f0608ff5e.jpg"};

DownloadUtil.downLoadToCompressFile(urls, targetFolderPath,newZipFilePath);

}

}

import java.io.*;

import java.net.URL;

import java.net.URLConnection;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.zip.CRC32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class DownloadUtil extends Thread {

public volatile static boolean exit = false;

//此方法实现文件夹的删除

public static void deleteAllFilesOfDir(File path) {

if (!path.exists())

return;

if (path.isFile()) {

path.delete();

return;

}

File[] files = path.listFiles();

for (int i = 0; i < files.length; i++) {

deleteAllFilesOfDir(files[i]);

System.out.println("删除图片" + files[i]);

}

path.delete();

}

//此方法实现图片下载

public static void download(String url,

String string,

String fname,

int skuId,

int index) throws IOException {

// TODO Auto-generated method stub

URL u = new URL(url);

URLConnection con = u.openConnection();

System.out.println("开始下载第" + index + " 图片");

try {

con.setConnectTimeout(20 * 1000);

con.setReadTimeout(10 * 1000);

} catch (Exception e) {

System.out.println("第" + index + "下载失败");

}

try {

InputStream is = con.getInputStream();

byte[] bs = new byte[1024];

int len;

File sf = new File(fname + skuId);

if (!sf.exists()) {

sf.mkdir();

}

OutputStream os = new FileOutputStream(sf.getPath() + '/' + string);

while ((len = is.read(bs)) != -1) {

os.write(bs, 0, len);

}

System.out.println("下载第" + index + "图片完成");

os.close();

is.close();

} catch (Exception e) {

// TODO: handle exception

}

}

//此方法实现文件夹得创建

public static boolean createDir(String fname) {

File dir = new File(fname);

if (dir.exists()) {

return false;

}

// 判断字符串是否以‘/’作为分隔符

if (!fname.endsWith(File.separator))

fname = fname + File.separator;

// 创建单个目录

return dir.mkdirs() ? true : false;

}

static final int BUFFER = 8192;

public static void compress(String srcPath , String dstPath) throws IOException{

File srcFile = new File(srcPath);

File dstFile = new File(dstPath);

if (!srcFile.exists()) {

throw new FileNotFoundException(srcPath + "不存在!");

}

FileOutputStream out = null;

ZipOutputStream zipOut = null;

try {

out = new FileOutputStream(dstFile);

CheckedOutputStream cos = new CheckedOutputStream(out, new CRC32());

zipOut = new ZipOutputStream(cos);

String baseDir = "";

compress(srcFile, zipOut, baseDir);

}

finally {

if(null != zipOut){

zipOut.close();

out = null;

}

if(null != out){

out.close();

}

}

}

private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException{

if (file.isDirectory()) {

compressDirectory(file, zipOut, baseDir);

} else {

compressFile(file, zipOut, baseDir);

}

}

/** 压缩一个目录 */

private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException{

File[] files = dir.listFiles();

for (int i = 0; i < files.length; i++) {

compress(files[i], zipOut, baseDir + dir.getName() + "/");

}

}

/** 压缩一个文件 */

private static void compressFile(File file, ZipOutputStream zipOut, String baseDir)  throws IOException{

if (!file.exists()){

return;

}

BufferedInputStream bis = null;

try {

bis = new BufferedInputStream(new FileInputStream(file));

ZipEntry entry = new ZipEntry(baseDir + file.getName());

zipOut.putNextEntry(entry);

int count;

byte data[] = new byte[BUFFER];

while ((count = bis.read(data, 0, BUFFER)) != -1) {

zipOut.write(data, 0, count);

}

}finally {

if(null != bis){

bis.close();

}

}

}

/**

*下载并压缩文件

*/

public static void downLoadToCompressFile(String[] picUrls, String fname, String newZipFilePath) throws IOException, InterruptedException {

final String targetFolderPath = fname;

final String[] url = picUrls;

DownloadUtil.createDir(targetFolderPath);

ExecutorService pool = Executors.newFixedThreadPool(6);

for (int i = 0; i < picUrls.length; i++) {

final int index = i;

pool.execute(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

try {

// 此方法实现图片的下载

String[] arr = url[index].split("\\.");

String suffix = arr[arr.length - 1];

DownloadUtil.download(url[index], index + "." + suffix, targetFolderPath, 1, index);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

}

//将目标目录的文件压缩成zip文件

compress(targetFolderPath , newZipFilePath);

DownloadUtil.sleep(5000);

DownloadUtil.exit = true;

//        File file = new File(fname);

//        DownloadUtil.deleteAllFilesOfDir(file);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值