这个工具类知道了已经要压缩的文件的路径,然后需要将这个路径下的文件进行压缩。
/** * 压缩下载照片 * * @param picUrl * @param response * @throws IOException */ public static void downloadPic(List<String> picUrl, HttpServletResponse response) throws IOException, AdminException { try { String downloadFilename = System.currentTimeMillis() + ".zip";//文件的名称 downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码 response.setContentType("application/octet-stream");// 指明response的返回对象是文件流 response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); String[] files = new String[picUrl.size()]; picUrl.toArray(files); for (int i = 0; i < files.length; i++) { String url = files[i]; zos.putNextEntry(new ZipEntry(downloadFilename + File.separator + i + ".jpg")); InputStream fis = new FileInputStream(new File(url)); byte[] buffer = new byte[1024]; int r = 0; while ((r = fis.read(buffer)) != -1) { zos.write(buffer, 0, r); } fis.close(); } zos.flush(); zos.close(); } catch (UnsupportedEncodingException e) { logger.error("不支持当前格式",e); } }