压缩文件zip格式,提供下载。

package com.bstd.sys.server;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import com.bstd.util.tool.MidLayer;
public class downImg extends HttpServlet {
    private String filePath = null;
    private String downFilePath=null;

    /**
     * Servlet标准Get方法
     * 
     */

    public void doGet(HttpServletRequest req, HttpServletResponse rep){
                  try {
                      // 1 先打包。
                    this.createZIPFile(req, rep);
                    //  2 在提供下载。 
                    this.downImg(req, rep); // 下载图片
                } catch (IOException e) {
                    e.printStackTrace();
                }
    }

    /**
     * Servlet标准Post方法
     * 
     */

    public void doPost(HttpServletRequest req, HttpServletResponse rep)
            throws ServletException, IOException {
        doGet(req, rep);
    }

    public void createZIPFile(HttpServletRequest req, HttpServletResponse rep) throws IOException{
            String nowpath = System.getProperty("user.dir");
            filePath = nowpath.replace("bin", "webapps");
            filePath += "\" + "tongjiImage";
            File file = new File(filePath) ;  // 定义要压缩的文件夹  
            downFilePath=filePath+"\统计图.zip";    // 定义压缩文件名称
            File zipFile = new File(downFilePath);   
            InputStream input = null ;  // 定义文件输入流  
            ZipOutputStream zipOut = null ; // 声明压缩流对象  
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile)) ;  
            zipOut.setComment("统计图打包,提供下载") ;  // 设置注释  
            int temp = 0 ;  
            if(file.isDirectory()){ // 判断是否是文件夹  
                File lists[] = file.listFiles() ;   // 列出全部文件  
                for(int i=0;i<lists.length;i++){  
                    input = new FileInputStream(lists[i]) ; // 定义文件的输入流  
                    zipOut.putNextEntry(new ZipEntry(file.getName()  
                        +File.separator+lists[i].getName())) ;  // 设置ZipEntry对象  
                    while((temp=input.read())!=-1){ // 读取内容  
                        zipOut.write(temp) ;    // 压缩输出  
                    }  
                    input.close() ; // 关闭输入流  
                }  
            }  
            zipOut.close() ;    // 关闭输出流  
        
    }
    public void downImg(HttpServletRequest req, HttpServletResponse rep){
        try {
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(downFilePath));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            rep.reset();

            OutputStream toClient = new BufferedOutputStream(rep.getOutputStream());
            rep.setContentType("application/zip");
            //如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
                rep.setHeader("Content-Disposition", "attachment;filename=" +URLEncoder.encode("统计图.zip", "UTF-8"));
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            } catch (IOException ex) {
            ex.printStackTrace();
            }finally{ //  下载之后,进行删除
                 try {
                        //File f = new File("E:\1111.zip");
                        //  f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
    }
    
     

}
原文地址:https://www.cnblogs.com/yongwuqing/p/5408711.html