多线程下载并压缩文件

demo

package com.ssh.gas.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.ssh.common.util.DownLoadUrl;
import com.ssh.common.util.DownLoadUrl2;
import com.ssh.common.util.PropertyFactory;
import com.ssh.common.util.ZipUtils;

public class Demo01 {
	public static void main(String[] args) throws InterruptedException {
		String[] str= {"HK06N124T92100000166;http://sxapi.pc.ehuatai.com:9004/service_platform/sxApp_getPdfEF.action?param=SEswNk4xMjRUOTIxMDAwMDAxNjYtMjJhZDg2YWRhMmMzMTZkNDFmMzc1ZGU0N2QyMzZhNjI=",
				"HK06N124T92100000168;http://sxapi.pc.ehuatai.com:9004/service_platform/sxApp_getPdfEF.action?param=SEswNk4xMjRUOTIxMDAwMDAxNjgtMDNhZGMwOWMwMjk5ZGM1YjI1NGZjOGNhYjg5OTM4ZjU=",
};
		System.out.println("下载开始时间:"+System.currentTimeMillis());
		 //线程池
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy()) {};
				CountDownLatch latch = new CountDownLatch(10); //这里创建类,指定几个线程数
		for (int j = 0; j < str.length; j++) {
			try {
				String[] split = str[j].split(";");
				//下载方法  非多线程调用
				//DownLoadUrl.downLoadByUrl(split[1],split[0]+".pdf","E:\\dow");
				System.out.println("保单号:"+split[0]+"下载地址:"+split[1]);
				//多线程 调用下载
				DownLoadUrl2 downLoadUrl2 = new DownLoadUrl2(split[1],split[0]+".pdf","E:\\dow",latch);
				threadPool.execute(downLoadUrl2);
				int activeCount = ((ThreadPoolExecutor)threadPool).getActiveCount();

				long completeTaskCount = ((ThreadPoolExecutor)threadPool).getCompletedTaskCount();

				int poolSize = ((ThreadPoolExecutor)threadPool).getPoolSize();

				long taskCount = ((ThreadPoolExecutor)threadPool).getTaskCount();

				long largestPoolSize = ((ThreadPoolExecutor)threadPool).getLargestPoolSize();

				long maximumPoolSize = ((ThreadPoolExecutor)threadPool).getMaximumPoolSize();
				System.out.println("当前活动的线程数:"+activeCount+"====完成任务线程数:"+completeTaskCount+"====核心线程数:"+poolSize+"====线程池中任务总量:"+taskCount+"过去执行最多的任务数"+largestPoolSize+"线程池中可以存放的最大的线程数"+maximumPoolSize);
				 int queueSize = threadPool.getQueue().size();
				    System.out.println("当前排队线程数:"+ queueSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		latch.await(); //当小于0 时执行 ,这个方法是阻塞的
		System.out.println("下载结束时间:"+System.currentTimeMillis());
		//打包
		ZipUtils.zipMultiFile("E:\\dow","E:\\dow\\8ad2599d4c654c349cf05e57182f7fef.zip");
		System.out.println("下载压缩时间"+System.currentTimeMillis());
	}
}




















下载url

package com.ssh.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CountDownLatch;

public class DownLoadUrl2 implements Runnable{

	/**
	   * 从网络Url中下载文件
     * @param urlStr
     * @param fileName
     * @param savePath
     * @throws IOException
     */
    public void  downLoadByUrl(String urlStr,String fileName,String savePath) throws IOException{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(10*1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);
        //文件保存位置
        File saveDir = new File(savePath);
        if(!saveDir.exists()){
            saveDir.mkdir();
        }
        File file = new File(saveDir+File.separator+fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if(fos!=null){
            fos.close();
        }
        if(inputStream!=null){
            inputStream.close();
        }
        System.out.println("info:"+url+" download success");

    }


    /**
            * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
    private String urlStr;
    private String fileName;
    private String savePath;
    private CountDownLatch latch;
	public DownLoadUrl2(String split, String string, String string2, CountDownLatch latch) {
		this.urlStr=split;
		this.fileName=string;
		this.savePath=string2;
		this.latch=latch;
		
	}
	@Override
	public void run() {
		try {
		    downLoadByUrl(urlStr,fileName,savePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 this.latch.countDown();
	}


	public String getUrlStr() {
		return urlStr;
	}


	public void setUrlStr(String urlStr) {
		this.urlStr = urlStr;
	}


	public String getFileName() {
		return fileName;
	}


	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


	public String getSavePath() {
		return savePath;
	}


	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
}

压缩文件

package com.ssh.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    public static void zipMultiFile(String filepath, String zippath) {
        try {
            File file = new File(filepath);// 要被压缩的文件夹
            File zipFile = new File(zippath);
            InputStream input = null;
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; ++i) {
                    input = new FileInputStream(files[i]);
                    zipOut.putNextEntry(new ZipEntry(/*file.getName() + File.separator + */files[i].getName()));
                    int temp = 0;
                    while ((temp = input.read()) != -1) {
                        zipOut.write(temp);
                    }
                    input.close();
                }
            }
            zipOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

压缩后如何返回

	 //返回给客户端
		FileDownLoadUtil fileDownLoadUtil=new FileDownLoadUtil();
		fileDownLoadUtil.fileDownLoad(request, response,PropertyFactory.getProperty("DownZipPdf")+ File.separator + C_BATCH+".zip");
		//删除文件夹
		FileUtilsDelete.delete(PropertyFactory.getProperty("DownZipPdf")+ File.separator +C_BATCH);
		//删除文件
		FileUtilsDelete.delete(PropertyFactory.getProperty("DownZipPdf")+ File.separator +C_BATCH+".zip");

下载返回客户端

package com.ssh.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * <p>
 * Description: 下载工具类
 * </p>
 * 
 * @author shenghuayao
 * @version 1.0 Create Date: 2013-3-22 上午11:47:05 Project Name: crm
 * 
 *          <pre>
 * Modification History: 
 *             Date                                Author                   Version          Description 
 * -----------------------------------------------------------------------------------------------------------  
 * LastChange: $Date:: 2013-03-22 #$      $Author: shenghuayao $          $Rev: 2478 $
 * </pre>
 * 
 */
public class FileDownLoadUtil {

	private Logger log = Logger.getLogger(FileUploadUtil.class);

	/**
	 * <p>
	 * 文件下载
	 * </p>
	 * 
	 * @author ShawnYao
	 * @date 2013-3-22 上午11:48:22
	 * @param request 
	 * @param response 
	 * @param filePath 文件路径
	 * @throws Exception
	 *             下载过程中出现异常
	 */
	public void fileDownLoad(HttpServletRequest request,
			HttpServletResponse response, String filePath) throws Exception {
		if (response != null && request != null && filePath != null
				&& !filePath.equals("")) {
			final String userAgent = request.getHeader("USER-AGENT");

			// filePath是指欲下载的文件的路径。
			//filePath=new String(filePath.getBytes("GBK"),"ISO8859_1");//转码后就行了
			filePath = URLDecoder.decode(filePath,"UTF-8");
			File file = new File(filePath);
			if(!file.exists()){
				log.info("############ FilePath: "+filePath);
				throw new Exception("文件不存在!");
			}
			if(!file.isFile()){
				log.info("############ FilePath: "+filePath);
				throw new Exception("非文件类型!");
			}
			
			// 取得文件名。
			String fileName = file.getName();
			if (userAgent.contains("MSIE")) {// IE浏览器
				fileName = URLEncoder.encode(fileName, "UTF8");
			} else if (userAgent.contains("Mozilla")) {// google,火狐浏览器
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} else {
				fileName = URLEncoder.encode(fileName, "UTF8");// 其他浏览器
			}
			// 提示框设置
			response.reset(); // reset the response
			// response.setCharacterEncoding("UTF-8");
			response.setContentType("application/octet-stream");
			response.setHeader("content-disposition", "attachment; filename=\""
					+ fileName + "\"");
			
			//读出文件到i/o流  
	        FileInputStream fis=new FileInputStream(file);  
	        BufferedInputStream buff=new BufferedInputStream(fis);  
	        byte [] ary_byte=new byte[1024];//缓存  
	        long k=0;//该值用于计算当前实际下载了多少字节  
			// 输出流
			OutputStream out =  response.getOutputStream();
			//开始循环下载  
	        while(k<file.length()){  
	            int j=buff.read(ary_byte,0,1024);  
	            k+=j;  
	            //将b中的数据写到客户端的内存  
	            out.write(ary_byte,0,j);  
	        }  
			// 关闭输出流
			if (out != null) {
				out.flush();
				out.close();
				fis.close();
				buff.close();
			}
			log.info("文件下载完毕!");
		} else {
			new NullPointerException(
					"HttpServletRequest Or HttpServletResponse Or fileName Is Null !");
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值