打包下载文件:
public String downZipDoc() throws IOException { HttpServletResponse response = ServletActionContext.getResponse(); //查询所有文档数据信息 docattachmentList = docAttachmentService.selectList(map); if (docattachmentList.size() > 0) { String newFilePath = null; List<File> fileList = new ArrayList<>(); //filePath 路径 for (Docattachment e : docattachmentList) { File file = new File(filePath + e.getFilePath()); if (!file.exists()) { file.createNewFile(); } //把保存的文件名称改为原来的名称 newFilePath = filePath + e.getFilePath().substring(0, e.getFilePath().lastIndexOf("/")) + "/" + e.getFileName() + e.getFilePath().substring(e.getFilePath().lastIndexOf(".")); File newFile = new File(newFilePath); file.renameTo(newFile); fileList.add(newFile); } String zipFileName = "压缩包名称"; DownloadFiles.downLoadFile1(fileList, response, zipFileName); } return null; }
工具类:
package com.gdzy.ZKZHFWPT.util; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import static com.gdzy.ZKZHFWPT.util.PropertiesUtil.getPropertiesValue; public class DownloadFiles { public static HttpServletResponse downLoadFile(List<File> files, HttpServletResponse response){ FileOutputStream fous = null; ZipOutputStream zipOut = null; String zipFilename = getPropertiesValue("tempZipPath") + "/" + System.currentTimeMillis() + ".zip"; try { File file = new File(zipFilename); if (!file.exists()){ file.createNewFile(); } response.reset(); //创建文件输出流 fous = new FileOutputStream(file); zipOut = new ZipOutputStream(fous); zipFile(files, zipOut); zipOut.close(); fous.close(); return downloadZip(file,response); }catch (Exception e) { e.printStackTrace(); } return response ; } /** * 把接受的全部文件打成压缩包 * @param List<File>; * @param org.apache.tools.zip.ZipOutputStream */ public static void zipFile (List<File> files,ZipOutputStream outputStream) { int size = files.size(); for(int i = 0; i < size; i++) { File file = (File) files.get(i); zipFile(file, outputStream); } } /** * 根据输入的文件与输出流对文件进行打包 * @param File * @param org.apache.tools.zip.ZipOutputStream */ public static void zipFile(File inputFile, ZipOutputStream ouputStream) { FileInputStream IN = null; BufferedInputStream bins = null; try { if(inputFile.exists()) { if (inputFile.isFile()) { IN = new FileInputStream(inputFile); bins = new BufferedInputStream(IN, 512); ZipEntry entry = new ZipEntry(inputFile.getName()); ouputStream.putNextEntry(entry); // 向压缩文件中输出数据 int nNumber; byte[] buffer = new byte[512]; while ((nNumber = bins.read(buffer)) != -1) { ouputStream.write(buffer, 0, nNumber); } // 关闭创建的流对象 bins.close(); IN.close(); } else { try { File[] files = inputFile.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(files[i], ouputStream); } } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } } public static HttpServletResponse downLoadFile1(List<File> files, HttpServletResponse response,String zipFileName){ FileOutputStream fous = null; ZipOutputStream zipOut = null; String zipFilename = getPropertiesValue("tempZipPath") + "/" + zipFileName + ".zip"; try { File file = new File(zipFilename); File filePath = new File(getPropertiesValue("tempZipPath")); if (!filePath.exists()){ filePath.mkdir(); } if (!file.exists()){ file.createNewFile(); } response.reset(); //创建文件输出流 fous = new FileOutputStream(file); zipOut = new ZipOutputStream(fous); zipFile(files, zipOut); zipOut.close(); fous.close(); return downloadZip(file,response); }catch (Exception e) { e.printStackTrace(); } return response ; } }