- /**
- * 批量文件打包下载
- * 具体实现步骤如下:
- * 1.设置下载文件名编码
- * 2.创建zip输出流ZipOutputStream
- * 3.将需要下载的文件流循环写入ZipOutputStream
- * 4.关闭各个流
- * @param path 下载文件的地址数组
- * @param response
- * @return
- * @throws IOException
- */
- public static boolean downLoadFileList(String[] path, HttpServletResponse response) throws IOException {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
- String downloadName = df.format(new Date()) + ".zip";
- response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(downloadName, "UTF-8"));
- FileSystem fs = FileSystem.get(conf);
- OutputStream out = response.getOutputStream();
- ZipOutputStream zos = new ZipOutputStream(out);
- for (int i = 0; i < path.length; i++) {
- InputStream in = fs.open(new Path(path[i]));
- String name = path[i].substring(path[i].lastIndexOf("/") + 1);
- byte[] buffer = new byte[1024];
- int len = 0;
- //创建zip实体(一个文件对应一个ZipEntry)
- ZipEntry entry = new ZipEntry(name);
- zos.putNextEntry(entry);
- //文件流循环写入ZipOutputStream
- while ((len = in.read(buffer)) != -1 ) {
- zos.write(buffer, 0, len);
- }
- in.close();
- zos.closeEntry();
- }
- zos.close();
- return true;
- }