Java动态生成文件、目录并压缩下载
动态生成Excel文件、目录
@GetMapping(value = "/downloadZip") public void downloadZips(HttpServletRequest request,HttpServletResponse response,Long taskId) throws IOException { List<taskFilesVO> vo=caseFilesService.getTaskFiles(taskId); String rootPath = request.getSession().getServletContext().getRealPath("/"); File temDir = new File(rootPath + "/" + "定价文件"); if(!temDir.exists()){ temDir.mkdirs(); } for (taskFilesVO taskvo:vo) { //动态生成目录 File folder = new File(temDir + "/" +taskvo.getWorkStepName()); if(!folder.exists()){ folder.mkdirs(); } if(taskvo.getWorkStepId()==0) createCoverExcel(folder+ "/封皮.xls",taskvo.getPrTaskDetail()); else if(taskvo.getWorkStepId()==1) createLogExcel(folder+ "/目录.xls",vo); else{ for (PrTaskFile file : taskvo.getPrTaskFileList()) { byte[] f=fileUploadService.downloadFile(file.getFileUrl());//将服务器文件下载到本地 FileOutputStream fos = new FileOutputStream(folder+ "/" +file.getFileName()); fos.write(f); } } } String zipFrontFile="定价文件.zip"; zipFrontFile = URLEncoder.encode(zipFrontFile,"UTF-8");//指定编码格式 response.setHeader("Content-Disposition","attachment; filename=" +zipFrontFile); response.setContentType("application/zip");// 定义输出类型 如果带了;号 IE下载会无效果 response.setStatus(200); ZipFilesUtil.toZip(temDir.getPath(), response.getOutputStream(),true); delFile(temDir); } //生成Excel public void createCoverExcel(String filename, taskDetailVO prTaskDetail) { try { FileOutputStream out = new FileOutputStream(filename); HSSFWorkbook workBook = new HSSFWorkbook(); HSSFSheet mySheet = workBook.createSheet(); workBook.setSheetName(0, "cover"); int rowNomber=-1; HSSFRow myRow = mySheet.createRow(++rowNomber); HSSFCellStyle style = workBook.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setWrapText(true); //设置字体样式 HSSFFont font = workBook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10); style.setFont(font); //设置标题行,每一列的标题 HSSFCell cell = myRow.createCell((short) 0); cell.setCellStyle(style); cell.setCellValue("政府制定价格卷宗"); CellRangeAddress region = new CellRangeAddress(0, 0, 0, 1); mySheet.addMergedRegion(region); for (int i = 1; i <= 5; i++) { HSSFRow row = mySheet.createRow(++rowNomber); HSSFCell cellfirst = row.createCell((short) 0); HSSFCell cellsecond = row.createCell((short) 1); if (i == 1) { cellfirst.setCellValue("定价项目"); cellsecond.setCellValue(prTaskDetail.getName()); } else if (i == 2) { cellfirst.setCellValue("定价单位"); cellsecond.setCellValue(prTaskDetail.getUnitName()); } else if (i == 3) { cellfirst.setCellValue("定价时间"); if(prTaskDetail.getFinishTime()!=null) { HSSFCellStyle dateCellStyle=workBook.createCellStyle(); short df=workBook.createDataFormat().getFormat("yyyy-mm-dd"); dateCellStyle.setDataFormat(df); String temp= new SimpleDateFormat("yyyy-MM-dd").format(prTaskDetail.getFinishTime()); cellsecond.setCellValue(temp); } } else if (i == 4) { cellfirst.setCellValue("定价文件编号"); cellsecond.setCellValue(prTaskDetail.getReferenceNumber()); } else if (i == 5) { cellfirst.setCellValue("保存期限"); cellsecond.setCellValue("20年"); } } workBook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } } //生成Excel public void createLogExcel(String filename, List<taskFilesVO> filesList) { try { //设置输出到本地的excel文件的名字和路径 FileOutputStream out = new FileOutputStream(filename); //生成excel文档对象 HSSFWorkbook workBook = new HSSFWorkbook(); //创建工作簿 HSSFSheet mySheet = workBook.createSheet(); //设置工作簿的名字 workBook.setSheetName(0, "catalog"); //创建第一行,标题行 int rowNomber=-1; HSSFRow myRow = mySheet.createRow(++rowNomber); HSSFCellStyle style = workBook.createCellStyle(); //设置字体样式 HSSFFont font = workBook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10); style.setFont(font); //设置标题行,每一列的标题 HSSFCell cell = myRow.createCell((short) 0); cell.setCellStyle(style); cell.setCellValue("序号"); cell = myRow.createCell((short) 1); cell.setCellStyle(style); cell.setCellValue("材料名称"); cell = myRow.createCell((short) 2); cell.setCellStyle(style); cell.setCellValue("页码"); cell = myRow.createCell((short) 3); cell.setCellStyle(style); cell.setCellValue("备注"); for(int i = 1; i <= filesList.size(); i++){ HSSFRow row = mySheet.createRow(++rowNomber); taskFilesVO file = filesList.get(i-1); //创建行中的列,并赋值 HSSFCell cellfirst = row.createCell((short) 0); cellfirst.setCellValue(i); HSSFCell cellsecond = row.createCell((short) 1); cellsecond.setCellValue(file.getWorkStepName()); } //写文件到本地 workBook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } } //删除本地文件及目录 public boolean delFile(File file) { if (!file.exists()) { return false; } if (file.isDirectory()) { File[] files = file.listFiles(); for (File f : files) { delFile(f); } } return file.delete(); }
//下载代码到本地
public byte[] downloadFile(String fileUrl) throws IOException {
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
return bytes;
}
压缩工具类
package com.panchina.util; import java.io.*; import java.util.List; import java.util.zip.*; public class ZipFilesUtil { private static final int BUFFER_SIZE = 2 * 1024; /** * 压缩成ZIP 方法1 * * @param srcDir * 压缩文件夹路径 * @param out * 压缩文件输出流 * @param KeepDirStructure * 是否保留原来的目录结构,true:保留目录结构; * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * @throws RuntimeException * 压缩失败会抛出运行时异常 */ public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure){ ZipOutputStream zos = null; try { zos = new ZipOutputStream(out); File sourceFile = new File(srcDir); compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure); } catch (Exception e) { e.printStackTrace(); } finally { if (zos != null) { try { zos.close();//最后必须关闭ZipOutputStream,否则可能压缩文件数据格式不正确 } catch (IOException e) { e.printStackTrace(); } } } } /** * * 递归压缩方法 * * @param sourceFile * 源文件 * * @param zos * zip输出流 * * @param name * 压缩后的名称 * * @param KeepDirStructure * 是否保留原来的目录结构,true:保留目录结构; * * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) * * @throws Exception * */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) { byte[] buf = new byte[BUFFER_SIZE]; try { if (sourceFile.isFile()) { // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip输出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { // 需要保留原来的文件结构时,需要对空文件夹进行处理 if (KeepDirStructure) { // 空文件夹的处理 zos.putNextEntry(new ZipEntry(name + "/")); // 没有文件,不需要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 判断是否需要保留原来的文件结构 if (KeepDirStructure) { // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } }catch(Exception e){ e.printStackTrace(); } } }
前端vue调用下载接口
downloadAll() { axios({ method: "GET", url: "/price/api/electronicFiles/downloadZip", params: { taskId: this.$route.query.id }, responseType: "blob" }) .then(res => { // console.log(res); /*let blob = new Blob([res.data], { type: "application/zip" }); let url = window.URL.createObjectURL(blob); window.location.href = url;*/ const fileName = res.headers["content-disposition"] .split("=")[1] .split("."); const _res = res.data; let blob = new Blob([_res]); let downloadName = decodeURIComponent(fileName[0]) + "." + fileName[1]; //下载后文件名 if ("msSaveOrOpenBlob" in navigator) { window.navigator.msSaveOrOpenBlob(blob, downloadName);//IE中使用Blob生成的是不带域名的blob链接,Google等浏览器生成的是带域名。所以在IE下通过a标签的href来下载是不行的。 } else { let downloadElement = document.createElement("a"); let href = window.URL.createObjectURL(blob); //创建下载的链接 downloadElement.href = href; downloadElement.download = downloadName; downloadElement.click(); //点击下载 window.URL.revokeObjectURL(href); //释放掉blob对象 } }) .catch(error => this.$message.error(error)); })