我们Java 日常开发的时候。经常会遇到各种导除,例如导除word 、excel等。
今天我们了解下。导除zip 格式。我们使用poi 新生成的文件,和服务器中存的文件,怎么同时压缩到一个文件夹中。
通常情况,我们是以流的形式,直接输出到前台。那么我们在做压缩文件的时候,就不应该把流输出到前台,而是加入到压缩包的流。
项目框架使用的是若依框架。实际情况根据自身决定,本人前台知识也比较薄弱。
下面我们上代码:
工具类
package cf.ding.web.utils;
import cf.ding.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
public class ZipUtils {
/**
* 设置返回前端文件名
* @param response response
* @param fileName 文件名,包含后缀
* @return OutputStream
* @throws Exception Exception
*/
public static OutputStream getOutputStreamFileName(HttpServletResponse response, String fileName) throws Exception {
response.reset();
String fileType = fileName.split("\\.")[1].toLowerCase();
switch (fileType){
case "doc":
response.setContentType("application/msword");//设置生成的文件类型
break;
case "docx":
response.setContentType("application/msword");//设置生成的文件类型
break;
case "xls":
response.setContentType("application/vnd.ms-excel");//设置生成的文件类型
break;
case "xlsx":
response.setContentType("application/vnd.ms-excel");//设置生成的文件类型
break;
case "pdf":
response.setContentType("application/pdf");//设置生成的文件类型
break;
case "zip":
response.setContentType("application/zip");//设置生成的文件类型
break;
case "dbf":
response.setContentType("application/x-dbf");//设置生成的文件类型
break;
default:
return response.getOutputStream();
}
response.setCharacterEncoding("UTF-8");//设置文件头编码方式和文件名
// response.setHeader("Content-Disposition", "attachment;filename=" +
// new String(URLEncoder.encode(fileName, "UTF-8").getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"utf8")); //刷新缓冲
return response.getOutputStream();
}
/**
* 压缩文件
* @param response response
* @param filePath 文件路径
* @param fileName 压缩生成文件名
* @throws IOException IOException
*/
public static void zipDateFile(HttpServletResponse response, String filePath, String fileName) throws Exception {
if (StringUtils.isEmpty(filePath) || !new File(filePath).exists()) return;
List<File> allFile = getAllFile(filePath);
zipDateFile(response, getAllFile(filePath), fileName, true);
}
/**
* 压缩文件
* @param response response
* @param fileList 文件集合
* @param fileName 压缩生成文件名
* @throws IOException IOException
*/
public static void zipDateFile(HttpServletResponse response, List<File> fileList, String fileName, boolean deleteSourceFile) throws Exception {
getOutputStreamFileName(response, fileName);
ServletOutputStream servletOutputStream = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(servletOutputStream);
zipFile(fileList, zipOutputStream, true);
try {
zipOutputStream.close();
} catch (IOException e) {
log.error("流关闭失败", e);
}
}
/**
* 压缩导出
* @param fileList 文件列表
* @param zipOutputStream zip流
* @throws IOException IOException
*/
public static void zipFile(List<File> fileList, ZipOutputStream zipOutputStream, boolean deleteSourceFile) throws IOException {
byte[] buffer = new byte[1024];
for (File file : fileList) {
if (file.exists()) {
if (file.isFile()) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
int size = 0;
while ((size = bis.read(buffer)) > 0) {