1.将文件压缩后下载工具类
package com.phkj.equipment.util.file;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final Logger log = LoggerFactory.getLogger(ZipUtils.class);
/**
* 多文件压缩
*
* @param
* @param outputStream
*/
public static void doCompress(List<FileBean> fileBeans, OutputStream outputStream) throws IOException {
ZipOutputStream out = null;
try {
//创建ZipOutPutStream流
out = new ZipOutputStream(outputStream);
doZip(out, fileBeans);
} catch (Exception e) {
log.error("Class ZipUtils Method doCompress error", e);
throw e;
} finally {
//关闭资源
if (out != null)
out.close();
}
}
/**
* @param out
* @param fileBeans 源文件路径,下载后的文件名字
* @throws IOException
*/
public static void doZip(ZipOutputStream out, List<FileBean> fileBeans) throws IOException {
FileInputStream fis = null;
try {
for (FileBean fileBean : fileBeans) {
File file = new File(fileBean.getImagePath());
if (file.exists()) {
//ZIP文件条目,设置条目名称
ZipEntry entry = new ZipEntry(fileBean.getFileName());
out.putNextEntry(entry);
int len = 0;
byte[] buffer = new byte[1024];
fis = new FileInputStream(file);
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
out.flush();
}
fis.close();
out.closeEntry();
}
}
} catch (Exception x) {
throw x;
} finally {
if (fis != null)
fis.close();
if (out != null)
out.closeEntry();
}
}
public static void main(String[] args) throws IOException {
List<FileBean> fileBeans = new ArrayList<>();
FileBean fileBean = new FileBean();
fileBean.setFileName("aaa.jpg");
fileBean.setImagePath("D:\\111\\20050616004330465927.jpg");
fileBeans.add(fileBean);
FileBean fileBean2 = new FileBean();
fileBean2.setFileName("bbb.jpg");
fileBean2.setImagePath("D:\\111\\20050616432730740583.jpg");
fileBeans.add(fileBean2);
File file = new File("D:/java.zip");
FileOutputStream fileOutputStream = new FileOutputStream(file);
doCompress(fileBeans, fileOutputStream);
}
}
2.文件类
package com.phkj.equipment.util.file;
import java.io.Serializable;
/**
* 文件实体类*/
public class FileBean implements Serializable {
private static final long serialVersionUID = -5452801884470115159L;
/**
* 源文件保存路径
*/
private String imagePath;
/**
* 下载后的文件名字
*/
private String fileName;
public FileBean(){
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
3.在控制器中使用
@RequestMapping(value = "/exportFaceZip", method = RequestMethod.GET)
public void exportFaceZip(HttpServletResponse response) throws Exception {
// 文件名外的双引号处理firefox的空格截断问题
response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
response.setContentType("application/x-msdownload");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
List<FileBean> fileBeans = new ArrayList<>();
ZipUtils.doCompress(fileBeans, response.getOutputStream());
}