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