java多文件打包下载


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.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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

import org.springframework.web.bind.annotation.RequestMapping;

import com.bb.base.common.Constant;

public class ZipUtils {        
    /**
     * 把接受的全部文件打成压缩包
     * @param List<File>;  
     * @param org.apache.tools.zip.ZipOutputStream  
     */
    public static void zipFile (List files,ZipOutputStream outputStream) {
        int size = files.size();
        for(int i = 0; i < size; i++) {
            File file = (File) files.get(i);
            zipFile(file, outputStream);
        }
    }
    /**  
     * 根据输入的文件与输出流对文件进行打包
     * @param File
     * @param org.apache.tools.zip.ZipOutputStream
     */
    public static void zipFile(File inputFile,  ZipOutputStream ouputStream) {
        try {
            if(inputFile.exists()) {
                if (inputFile.isFile()) {
                    FileInputStream IN = new FileInputStream(inputFile);
                    BufferedInputStream bins = new BufferedInputStream(IN, 512);
                    ZipEntry entry = new ZipEntry(inputFile.getName());
                    ouputStream.putNextEntry(entry);
                    // 向压缩文件中输出数据   
                    int nNumber;
                    byte[] buffer = new byte[512];
                    while ((nNumber = bins.read(buffer)) != -1) {
                        ouputStream.write(buffer, 0, nNumber);
                    }
                    // 关闭创建的流对象   
                    bins.close();
                    IN.close();
                } else {
                    try {
                        File[] files = inputFile.listFiles();
                        for (int i = 0; i < files.length; i++) {
                            zipFile(files[i], ouputStream);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
        if(file.exists() == false){  
            System.out.println("待压缩的文件目录:"+file+"不存在.");  
        }else{
            try {
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
    
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
    
            //如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            } catch (IOException ex) {
            ex.printStackTrace();
            }finally{
                 try {
                        File f = new File(file.getPath());
                        f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
        }
        return response;
    }
    
    
    
    
    //应用场景 打包 
    @RequestMapping(value = "downloadZip")
    public HttpServletResponse downloadZip(HttpServletRequest request,HttpServletResponse response)throws Exception {
        List<File> files = new ArrayList<File>();
        //打包凭证.zip与EXCEL一起打包
        try {
            String zipFilenameA =  Constant.QRC_LOCALPATH + "/tempFileA.zip" ;
            File fileA = new File(zipFilenameA);
            if (!fileA.exists()){   
                fileA.createNewFile();   
            }
            response.reset();
            //response.getWriter()
            //创建文件输出流
            FileOutputStream fousa = new FileOutputStream(fileA);   
            ZipOutputStream zipOutA = new ZipOutputStream(fousa);
            ZipUtils.zipFile(files, zipOutA);
            zipOutA.close();
            fousa.close();
           return ZipUtils.downloadZip(fileA,response);
        }catch (Exception e) {
                e.printStackTrace();
            }
        return response ;
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM