接上一篇[http://blog.csdn.net/mr_liud/article/details/78468643]来实现多个word文档打包生成压缩文件并下载。
话不多说:下面是代码
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
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 org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Created by LiuD on 2017/11/7
*/
public class TestZip {
/**
* 批量下载
*/
@ResponseBody
@RequestMapping("/download.ajax")
public class download(HttpServletResponse response) {
List<File> list = new ArrayList<>();
String s1 = "好好学习";
String fileName1 = s1 + ".doc";
File file1 = new File(fileName1);
htmlToDoc(s1, file1);
list.add(file1);
String s2 = "天天向上";
String fileName2 = s2 + ".doc";
File file2 = new File(fileName2);
htmlToDoc(s2, file2);
list.add(file2);
String s3 = "好好学习12321AA";
String fileName3 = s3 + ".doc";
File file3 = new File(fileName3);
htmlToDoc(s3, file3);
list.add(file3);
try {
// 要压缩的文件路径
File file = new File("e:\\1.zip");
if (file.exists()) {
System.out.println("该文件名已存在。");
} else {
boolean newFile = file.createNewFile();
if (newFile) {
System.out.println("恭喜你,下载成功");
} else {
System.out.println("下载失败。请稍后再试···");
}
}
//创建文件输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
// ZipOutputStream输出流转换
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
// 接收文件集合、压缩流
zipFileAll(list, zipOutputStream);
zipOutputStream.close();
fileOutputStream.close();
downloadZip(file, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把接收的全部文件打成压缩包
*
* @param files (文件集合)
* @param outputStream (压缩流、输出)
*/
private static void zipFileAll(List files, ZipOutputStream outputStream) {
for (Object file1 : files) {
File file = (File) file1;
zipFile(file, outputStream);
}
}
/**
* 根据输入的文件与输出流对文件进行打包
*
* @param file (单个文件对象)
* @param outputStream (压缩流、输出)
*/
private static void zipFile(File file, ZipOutputStream outputStream) {
try {
if (file.exists()) {
if (file.isFile()) {
FileInputStream IN = new FileInputStream(file);
BufferedInputStream bins = new BufferedInputStream(IN, 1024);
// 将文件名写入压缩文件中
ZipEntry entry = new ZipEntry(file.getName());
outputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[1024];
while ((nNumber = bins.read(buffer)) != -1) {
outputStream.write(buffer, 0, nNumber);
}
outputStream.flush();
bins.close();
IN.close();
} else {
File[] files = file.listFiles();
for (File file1 : files) {
zipFile(file1, outputStream);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下载文件,支持跨域
*
* @param file (要下载的文件)
* @param response
*/
privtal static void downloadZip(File file, HttpServletResponse response) {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
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();
}
}
}
}