前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 下载zip文件

Java 下载zip文件

作者头像
凯哥Java
发布2019-06-30 11:20:39
7.3K2
发布2019-06-30 11:20:39
举报
文章被收录于专栏:凯哥Java凯哥Java

选中文件打zip包下载

前台:文件列表,选中文件,传文件ID集合传到后台

后台:

//接受到文件ID集合,查询出文件urlList

List attachFileList = bookService.findAttachFileList(ids);

List strFiles = new ArrayList();

for(AttachFile attachFile : attachFileList){

strFiles.add(attachFile.getAttachFileUrl());

}

//调用打包工具类进行下载

ZipUtil.ZipFiles(getRequest(), getResponse(), strFiles, UUID.randomUUID().toString()+".zip");

直接下载zip包  zipUtil也有

/////////////////////////////////zipUtil  ↓↓↓    ////////////////////

package com.jk.book.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

/**

* 把文件打成压缩包并保存在本地硬盘

*

* @param srcfile

* @param zipPath

*/

public static void ZipFiles(List srcfiles, String zipPath) {

    byte[] buf = new byte[4096];

    ZipOutputStream out = null;

    try {

      // 创建zip输出流

      out = new ZipOutputStream(new FileOutputStream(zipPath));

      // 循环将源文件列表添加到zip文件中

      for (int i = 0; i < srcfiles.size(); i++) {

    File file = new File(srcfiles.get(i));

        FileInputStream in = new FileInputStream(file);

        String fileName = file.getName();

        // 将文件名作为zip的Entry存入zip文件中

        out.putNextEntry(new ZipEntry(fileName));

        int len;

        while ( (len = in.read(buf)) > 0) {

          out.write(buf, 0, len);

        }

        out.closeEntry();

        in.close();

      }

    }

    catch (IOException e) {

      e.printStackTrace();

    } finally {

    if (null != out) {

    try {

out.close();

out = null;

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }

    }

}

/**

* 把文件打成压缩包并输出到客户端浏览器中

*

* @param request

* @param response

* @param srcFiles

* @param downloadZipFileName

*/

public static void ZipFiles(HttpServletRequest request, HttpServletResponse response, List srcFiles, String downloadZipFileName) {

    byte[] buf = new byte[4096];

    try {

      // Create the ZIP file

    // ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));

    ZipOutputStream out = new ZipOutputStream(response.getOutputStream());//--设置成这样可以不用保存在本地,再输出, 通过response流输出,直接输出到客户端浏览器中。

      // Compress the files

    if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {

    downloadZipFileName = new String(downloadZipFileName.getBytes("GB2312"),"ISO-8859-1");

        } else {

        // 对文件名进行编码处理中文问题

        downloadZipFileName = java.net.URLEncoder.encode(downloadZipFileName, "UTF-8");// 处理中文文件名的问题

        downloadZipFileName = new String(downloadZipFileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题

        }

    response.reset(); // 重点突出

        response.setCharacterEncoding("UTF-8"); // 重点突出

        response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出

        // inline在浏览器中直接显示,不提示用户下载

        // attachment弹出对话框,提示用户进行下载保存本地

        // 默认为inline方式

        response.setHeader("Content-Disposition", "attachment;filename="+downloadZipFileName);

      for (int i = 0; i < srcFiles.size(); i++) {

    File file = new File(srcFiles.get(i));

        FileInputStream in = new FileInputStream(file);

        // Add ZIP entry to output stream.

        String fileName = file.getName();

        out.putNextEntry(new ZipEntry(fileName));

        // Transfer bytes from the file to the ZIP file

        int len;

        while ( (len = in.read(buf)) > 0) {

          out.write(buf, 0, len);

        }

        // Complete the entry

        out.closeEntry();

        in.close();

      }

      // Complete the ZIP file

      out.close();

      System.out.println("压缩完成.");

    }

    catch (IOException e) {

      e.printStackTrace();

    }

}

public static void main(String[] args) {

List srcFiles = new ArrayList();

srcFiles.add("c:/codetemplates.xml");

srcFiles.add("d:/t1.png");

ZipFiles(srcFiles, "c:/mm.zip");

}

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档