選中文件打zip包下載
前臺:文件列表,選中文件,傳文件ID集合傳到后臺
后臺:
//接受到文件ID集合,查詢出文件urlList
List attachFileList = bookService.findAttachFileList(ids);
List strFiles = new ArrayList();
for(AttachFile attachFile : attachFileList){
strFiles.add(attachFile.getAttachFileUrl());
}
//調(diào)用打包工具類進(jìn)行下載
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 {
? ? ? // 創(chuàng)建zip輸出流
? ? ? out = new ZipOutputStream(new FileOutputStream(zipPath));
? ? ? // 循環(huán)將源文件列表添加到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());//--設(shè)置成這樣可以不用保存在本地,再輸出, 通過response流輸出,直接輸出到客戶端瀏覽器中。
? ? ? // Compress the files
? ? if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
? ? downloadZipFileName = new String(downloadZipFileName.getBytes("GB2312"),"ISO-8859-1");
? ? ? ? } else {
? ? ? ? // 對文件名進(jìn)行編碼處理中文問題
? ? ? ? downloadZipFileName = java.net.URLEncoder.encode(downloadZipFileName, "UTF-8");// 處理中文文件名的問題
? ? ? ? downloadZipFileName = new String(downloadZipFileName.getBytes("UTF-8"), "GBK");// 處理中文文件名的問題
? ? ? ? }
? ? response.reset(); // 重點(diǎn)突出
? ? ? ? response.setCharacterEncoding("UTF-8"); // 重點(diǎn)突出
? ? ? ? response.setContentType("application/x-msdownload");// 不同類型的文件對應(yīng)不同的MIME類型 // 重點(diǎn)突出
? ? ? ? // inline在瀏覽器中直接顯示,不提示用戶下載
? ? ? ? // attachment彈出對話框,提示用戶進(jìn)行下載保存本地
? ? ? ? // 默認(rèn)為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");
}
}