利用压缩包的形式进行批量下载
/*** 将磁盘的多个文件打包成压缩包并输出流下载** @param pathList* @param response*/public void zipDirFileToFile(List<Map<String, String>> pathList, HttpServletResponse response, String taskName) {try {// 设置response参数并且获取ServletOutputStreamZipArchiveOutputStream zous = getServletOutputStream(response, taskName);for (Map<String, String> map : pathList) {String fileName = map.get("outputName");File file = new File(map.get("localFileName"));InputStream inputStream = new FileInputStream(file);setByteArrayOutputStream(fileName, inputStream, zous);}zous.close();} catch (Exception e) {e.printStackTrace();}}public ZipArchiveOutputStream getServletOutputStream(HttpServletResponse response, String taskName) throws Exception {//输出压缩文件名年月日时分秒String outputFileName = taskName + ".zip";Log.info("压缩文件名:" + outputFileName);response.reset();response.setHeader("Content-Type", "application/octet-stream");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(outputFileName, "UTF-8"));response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");ServletOutputStream out = response.getOutputStream();ZipArchiveOutputStream zous = new ZipArchiveOutputStream(out);zous.setUseZip64(Zip64Mode.AsNeeded);return zous;}public void setByteArrayOutputStream(String fileName, InputStream inputStream, ZipArchiveOutputStream zous) throws Exception {ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer = new byte[5120];int len;while ((len = inputStream.read(buffer)) != -1) {baos.write(buffer, 0, len);}baos.flush();byte[] bytes = baos.toByteArray();//设置文件名ArchiveEntry entry = new ZipArchiveEntry(fileName);zous.putArchiveEntry(entry);zous.write(bytes);zous.closeArchiveEntry();baos.close();inputStream.close();}
