日期:2014-05-16  浏览次数:20774 次

org.apache.tools.zip.*和org.apache.commons.httpclient.*实现远程文件打包下载,支持中文文件名
package com.kedacom.kdkk.controller.querymanager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

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

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

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

public class BatchDownload extends HttpServlet {
	
private static List list = new ArrayList();
Date date = null;
private static int BUF_SIZE = 40480;  
private static String ZIP_ENCODEING = "GBK";  


 public BatchDownload() {
  super();
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
	//设置编码
	request.setCharacterEncoding("utf-8");
	date =  new Date();
	//设置下载头信息
	response.setContentType("application/octet-stream");
	response.setHeader("Content-Disposition", "attachment; filename=\""+date.getTime()+".zip\"");
	//要打包下载的图片json参数
  	String cbxStr = request.getParameter("cbxStr");
  	if(cbxStr.length() > 0){
  		cbxStr = cbxStr.substring(0, cbxStr.length()-1);
  		String [] cbxs = cbxStr.split(";");
  	     HttpClient client = new HttpClient();
  	     GetMethod get = null;
		 //创建本地存储文件路径
  	     new File("d:/vehicleImgs/"+date.getTime()+"/").mkdir();
  	     for(int i = 0; i < cbxs.length; i ++){
  	    	 try {   
				 //构建远程服务的图片下载路径
  	    		 String cbxs2 [] = cbxs[i].split("z");
  	    		 String vid = cbxs2[0];
  	    		 String timestamp = cbxs2[1];
  	    		 String imgtype = cbxs2[2].split(",")[0];
  	    		 String imgName = cbxs2[3];
  	    		 imgName = imgName.replace(" ", "_").replace(":", "_");
  	    		 DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  	    		 Long timeStamp = format1.parse(timestamp).getTime();
  	    		 String imgUrl = getServletContext().getInitParameter("opticmServer");                 
				 //此方法的参数可自行传入,参数值为远程服务的servlet(xxx.do),返回的值为文件流对象
  			     get = new GetMethod(imgUrl+"&contentId="+vid+"&imgType="+imgtype+"&imgOrder=0&timeStamp="+timeStamp);    
  			     int j = client.executeMethod(get);                               
  			     if (200 == j)//是否正确返回                                                    
  			      {                                                               
  			          File storeFile = new File("d:/vehicleImgs/"+date.getTime()+"/"+imgName+".jpg");
  			          FileOutputStream output = new FileOutputStream(storeFile);  
  			          // 得到网络资源的字节数组,并写入文件                        
  			          output.write(get.getResponseBody());                        
  			          output.close();      
  			      }else{                                                               
  			          System.out.println("no pic");                               
  			      }                                                           
  			  } catch ( Exception e ){                                                                   
  			      System.out.println("Exception no pic");                                   
  			  } finally{
  				  get.releaseConnection(); 
  			  }
  	     }
  	   try {
		   //开始压缩下载下来的本机图片
  		  zip("d:\\vehicleImgs\\"+date.getTime()+".zip", new File("d:\\vehicleImgs\\"+date.getTime()+"\\"));
		  //下载zi