@RequestMapping(value = "/downzip", method = RequestMethod.GET)
public void downzip(HttpServletRequest request,HttpServletResponse response) throws Exception {
List files = new ArrayList();
for(int a=0;a<2;a++){
String excel = "classpath:static/111.xls";
File fi = ResourceUtils.getFile(excel);
//File fi = new File(excel);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
// 读取excel模板
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// HSSFCellStyle cellStyle = wb.createCellStyle();
// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
sheet.createRow(0).createCell(0).setCellValue("标题111");
for(int i=2; i<3; i++) {
HSSFRow row = sheet.createRow(i);
row.createCell(0).setCellValue("11");
row.createCell(1).setCellValue("22");
row.createCell(2).setCellValue("33");
row.createCell(3).setCellValue("44");
row.createCell(4).setCellValue("55");
row.createCell(5).setCellValue("66");
row.createCell(6).setCellValue("77");
}
String pathfile= SaveWorkbook(wb);
files.add(pathfile);
System.out.println(pathfile);
}
long nowTime = new Date().getTime();
String name = "attachment_" + nowTime + ".zip";
String path = fileToZip(files,name);
// String path ="E:\\home\\attachment_1568345018284.zip";
OutputStream out = null;
BufferedInputStream br = null;
try {
String fileName = URLEncoder.encode(name, "UTF-8");
br = new BufferedInputStream(new FileInputStream(path));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
// 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
out.flush();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
br.close();
out.close();
}
}
private String SaveWorkbook(HSSFWorkbook workbook )throws Exception{
// SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddhhmmss");
String fileName = new Date().getTime()+"";
String newPath ="E:\\home\\"+fileName+".xls";
FileOutputStream fileOut = new FileOutputStream(newPath );
workbook.write(fileOut);
fileOut.close();
return newPath;
}
public static String fileToZip(List list,String fileName) {
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
// 临时目录 C:\Users\ADMINI~1\AppData\Local\Temp\
String path ="E:\\home\\"+ fileName;
try {
File zipFile = new File(path );
// 如果存在该zip文件,则删除
zipFile.deleteOnExit();
// 创建一个新文件
zipFile.createNewFile();
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 10];
for (int i=0;i<list.size();i++) {
File subFile = new File(list.get(i).toString());
//如果文件不存在,则不添加到zip包中
if (!subFile.exists()){
continue;
}
// 文件名增加时间戳避免重复
String subFileName = list.get(i).toString();
String temp[] = subFileName.split("\\\\");
String subFileName1 = temp[temp.length-1];
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(subFileName1);
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(subFile);
bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
}
System.out.println("压缩成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
//关闭流
try {
if (null != bis) bis.close();
if (null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return path;
}